Professional Documents
Culture Documents
Exam
Exam
INTRODUCTION TO PROGRAMMING
FINAL EXAMINATION
July, 2022
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 21 21 30 18 10 100
Obtained points
Question 1 (21 marks)
Identify and correct the errors in each of the following statements:
c) if (c < 7);{
printf( ”C is less than 7\ n” );
}
d) if ( c => 7 ) {
printf( ”C is greater than or equal to 7\n” );
}
e) while ( c <= 5 ) {
product ∗ = c;
+ + c;
f) if (gender == 1 )
puts( ”Woman” );
else;
puts( ”Man” );
Answer:
Student ID:
#include<stdio.h>
int main()
{
unsigned int counter; //number of grade to be entered next
int grade;
int total;
int average;
//initialization phase
total=0; //initialize total
counter=1;//initialize loop counter
while ( counter <=10 )
{ // loop 10 times
printf( ”%s”, ”Enter grade: ” ); // prompt for input
scanf( ”%d”, &grade ); // read grade from user
total = total + grade; // add grade to total
counter = counter + 1; // increment counter
}
// termination phase
average = total / 10; // integer division
printf( ”Class average is %d \n”, average ); // display result
}
Answer:
2
Student ID:
b)
#include <stdio.h>
int main()
{
int n[5]= {32, 27, 64, 18, 95}; // n is an array of five integers
// set elements of array n to 0
for ( int i = 0; i< 5; ++i) {
printf(”%d %d\n”, i, n[i]);
}
}
Answer:
3
Student ID:
c)
#include<stdio.h>
int main(void)
{
int a=7;
int ∗ aPtr = &a; // set aPtr to the address of a
printf(”The address of a is %p”
”\nThe value of aPtr is %p”,&a, aPtr);
printf(”\n\nThe value of a is %d”
”\nThe value of ∗ aPtr is %d”, a, ∗ aPtr);
printf(”\n\nShowing that ∗ and & are complements of ”
”each other \n&∗ aPtr = %p”
”\n∗ &aPtr = %p\n”, &∗ aPtr, ∗ &aPtr);
}
Answer:
4
Student ID:
#include<stdio.h>
int maximum(int x, int y, int z);
int main()
{
int number1;
int number2;
int number3;
printf(”%s”, ”Enter three integers: ”);
scanf(”%d%d%d”, &number1, &number2, &number3);
// number1, number2 and number3 are arguments
// to the maximum function call
printf(”Maximum is: %d\n”,maximum(number1, number2, number3));
}
// Function maximum definition
// x, y and z are parameters
.............................................
.............................................
.............................................
.............................................
.............................................
.............................................
.............................................
.............................................
.............................................
5
Student ID:
b)
#include<stdio.h>
long long int fibonacci(int n); // function prototype
int main(void)
{
int number; // number input by user
// obtain integer from user
printf(”%s”, ”Enter an integer: ”);
scanf(”%d”, &number);
// calculate fibonacci value for number input by user
long long int result = fibonacci(number);
// display result
printf(”Fibonacci(%d) = %lld\n”, number, result);
}
// Recursive definition of function fibonacci
.............................................
.............................................
.............................................
.............................................
.............................................
.............................................
.............................................
.............................................
.............................................
6
Student ID:
c)
#include<stdio.h>
void cubeByreference( int ∗ nPtr); // function prototype
int main(void)
{
int number=5; // initialize number
printf(”The original value of number is %d”, number);
// pass address of number to cubeByReference
.............................................
printf(”\nThe new value of number is %d\n”, number);
}
// calculate cube of ∗ nPtr; actually modifies number in main
.............................................
.............................................
.............................................
.............................................
7
Question 4 (total 18 marks)
Write a function that reads three nonzero integers and determines whether they are the
sides of a right-angled triangle. The function should take three integer arguments and
return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise.
Use this function in a program that inputs a series of sets of integer.
Question 5 (total 10 marks)
Find the errors and correct them in each of the following:
a) union values {
char w;
float x;
double y;
};
union values v = { 1.27 };
b) struct person {
char lastName[15];
char firstName[15];
unsigned int age;
}
Student ID:
BLANK PAGE FOR EXTRA WORKING (Any work here will NOT be marked.)
10