Exam
Exam
Exam
INTRODUCTION TO PROGRAMMING
FINAL EXAMINATION
August, 2023
Duration: 90 minutes
Full name:....................................................
Student ID:....................................................
INSTRUCTIONS
4. All calculators are permitted. All other handheld devices are NOT allowed.
Question 1 2 3 4 5 sum
Max. points 24 21 30 11 14 100
Obtained points
Question 1 (24 marks)
Identify and correct the errors in each of the following:
a) while ( c =< 5 )
{
product *= c
c++;
}
d) int b[10] = { 0 }, i;
for (i = 0; i <= 10; i++)
{
b[i] = 1;
}
e) int number;
printf(”%d \n”, number);
f) float x = 19.34;
float xPtr = &x;
printf(”%f\n”, xPtr);
Student ID:
i) i=5, j=3;
printf(”%d %d”, i/j, i%j);
Answer:
b)
i=1;
while( i <= 28)
{
printf(”%d”,i);
i *=2;
}
Answer:
2
Student ID:
c)
i=938;
do
{
printf(”%d”,i);
i/=10;
}while(i>0);
Answer:
d)
for( i=5,j=i-1;i>0,j>0;i–,j=i-1)
printf(”%d”,i);
Answer:
3
Student ID:
#include<stdio.h>
int sum(int n);
int main()
{
int number, result;
printf(”Enter a positive integer: ”);
.............................................
.............................................
printf(”sum = %d”, result);
return 0;
}
int sum(int n)
{
if (n ! = 0)
// sum() function calls itself
.............................................
else
.............................................
}
4
Student ID:
#include<stdio.h>
float calculateSum(float num[]);
int main()
{
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};
// num array is passed to calculateSum()
.............................................
printf(”Result = %.2f”, result);
return 0;
}
float calculateSum(float num[])
{
.............................................
.............................................
{
.............................................
}
.............................................
}
5
Student ID:
c)
#include<stdio.h>
int main()
{
char name[50];
int marks, i, num;
printf(”Enter number of students: ”);
scanf(”%d”, &num);
.............................................
fptr = (fopen(”C:\ \student.txt”, ”a”));
.............................................
{
printf(”Error!”);
exit(1);
}
for(i = 0; i < num; i++)
{
printf(”For student %d \n Enter name: ”, i+1);
.............................................
printf(”Enter marks: ”);
.............................................
fprintf(fptr,”\n Name: %s \nMarks=%d\n”, name, marks);
}
.............................................
return 0;
}
6
Question 4 (total 11 marks)
Write a function to find the greatest common factor of two given numbers using Recur-
sion.
Student ID:
#include<stdio.h>
int main()
{
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
ptr = &x[2];
printf(”*ptr = %d \n”, *ptr);
printf(”(ptr+1) = %d \n”, *(ptr+1));
printf(”(ptr-1) = %d”, *(ptr-1));
return 0;
}
Answer:
8
Student ID:
b)
#include<stdio.h>
#include <string.h>
Answer:
9
BLANK PAGE FOR EXTRA WORKING (Any work here will NOT be marked.)