You are on page 1of 3

Example Exam Programming 2 (version 13-1-2020)

The questions in this example exam form a representative set.

Open questions (total 100 points)

Topic: Functions (25 points)

1. Given the following program:

#include <stdio.h>

#define MAX_GRADES 10

float grades[MAX_GRADES] = {0.0};

int main()
{
int index;
float entered_value;
unsigned int nr_grades;
float total = 0.0;
int nr_passed_students = 0;
float average;

for (index = 0; index < MAX_GRADES; index++)


{
printf("Enter a grade: ");
scanf("%f", &entered_value);

if (entered_value < 0.0)


{
break;
}
else
{
grades[index] = entered_value;
}
}

nr_grades = index;

for (index = 0; index < nr_grades; index++)


{
total = total + grades[index];
}

LED - Example exam Programming 2 (version: 13-1-2020) 1


if (nr_grades > 0)
{
average = total / nr_grades;
}
else
{
average = 0.0;
}

for (index = 0; index < nr_grades; index++)


{
if (grades[index] >= 5.5)
{
nr_passed_students++;
}
}

for (index = 0; index < nr_grades; index++)


{
printf("Grade %d: %f\n", index + 1, grades[index]);
}

if (nr_grades > 0) printf("Average = %f\n", average);

printf("Number of passed students: %d\n",


nr_passed_students);

return 0;
}

a) Describe the four functional parts within the given source code.

b) The parts identified in a) will be written in functions of a new program.

First determine the arguments and the return type of each function.
Choose meaningful names for the functions and the arguments.

Give the declarations (prototypes) of these functions. Describe the


meaning of each argument and the return value. You do not have to give
the implementation (function body) of the functions.

Important: In the new program, the already existing global array should
not been changed and also not any additional global variables should be
used.

c) Write the main function of the new program calling the new functions.

LED - Example exam Programming 2 (version: 13-1-2020) 2


Topic: Binary numeral system & operators (25 points)

2. Write a program that finds the position of the most significant bit set to 1
in an unsigned integer.

Important: The program is not allowed to use any library functions to


find the most significant bit.

Topic: Pointers (25 points)

3. Write a function that swaps the values of two variables of the type int.
The function has two arguments to pass the addresses of the variables
(pass by reference).

Show the function call to swap the values of the following integer
variables:

int x = 1;
int y = 2;

Topic: Characters and strings (25 points)

4. A program contains the following string:

char text[] = "Convert this to uppercase!";

Write a program that converts all the letters of this string to uppercase.
Make your program generic such that it can handle other text in the
string, containing other characters and of different sizes.

Important: In the program it is not allowed to use a library function that


converts the letters to uppercase.

LED - Example exam Programming 2 (version: 13-1-2020) 3

You might also like