CCP4114 - PROGRAM DESIGN LAB 8
NOTE:
- Create a folder on Desktop to save your
works. Name the folder appropriately (e.g. Lab
2) REMINDER!
Write your
- Use comment // to write your name, ID, Group
particulars in the
and Lab Question in each program.
program.
- Save your file as .c
LAB OBJECTIVES
At the end of this lab activity, the students should be able to:
● Solve two-dimensional array problem
● Use pointers expressions and pointer arithmetic.
Term 2420 1
CCP4114 - PROGRAM DESIGN LAB 8
ARRAY
QUESTION 1
This program will calculate the average of quiz marks for students.
In main():
o Declare an empty array called test for 4 students with 3 quizzes each.
o Call function get_marks(…) and pass the test array as argument.
o Use while loops:
Calculate the total marks and average for each students.
Call function display(…) and pass the necessary arguments.
In function get_marks(…):
o Use for loops to ask each user to enter their marks.
o The marks are stored into the array.
In function display(…):
o Print the average of each students.
(There are no loops here, just a print statement.)
Sample Output
Student 1 :
Enter quiz 1 marks : 6.5
Enter quiz 2 marks : 8
Enter quiz 3 marks : 10
Student 2 :
Enter quiz 1 marks : 7.5
Enter quiz 2 marks : 7
Enter quiz 3 marks : 8
Student 3 :
Enter quiz 1 marks : 4
Enter quiz 2 marks : 8
Enter quiz 3 marks : 10
Student 4 :
Enter quiz 1 marks : 6
Enter quiz 2 marks : 7
Enter quiz 3 marks : 6.5
Student 1 average ==> 8.17
Student 2 average ==> 7.50
Student 3 average ==> 7.33
Student 4 average ==> 6.50
Term 2420 2
CCP4114 - PROGRAM DESIGN LAB 8
POINTER
QUESTION 2
Trace the following code segments and write the outputs generated by them.
(a) void f(int* p, int m)
{
m = m + 25;
*p = *p + ++m;
return;
}
void main()
{
int i=5, j=10;
f(&i, j);
printf("%d", i+j);
}
(b) int x=337;
void Q(int z)
{
z += x;
print(z);
}
void P(int *y)
{
int x = *y + 8;
Q(x);
*y = x - 3 * x;
print(x);
}
void print(int val)
{
printf("\n%d", val);
}
void main()
{
x = 5;
P(&x);
print(x);
}
Term 2420 3
CCP4114 - PROGRAM DESIGN LAB 8
(c) int numbers[]={10,8,34,100,3,12,14,7};
int *a, *b;
a = &numbers[3];
b = numbers;
printf("\n %d", *a + *b);
printf("\n %d", *a-- - ++*b);
printf("\n %d", *(a+2) + *b+15);
(d) int arr[] = {1,2,3};
main()
{
int *ptr;
ptr = arr;
ptr = ptr+3;
printf("%d",*ptr);
}
(e) int fun(int *a,int *b)
{
*a = *a+*b;
*b = *a-*b;
*a = *a-*b;
}
main()
{
int x = 10,y = 20;
fun(&x,&y);
printf("x= %d y = %d\n", x, y);
}
Term 2420 4