You are on page 1of 30

Programming 2

Lecture week 2.7: example exam

Erik Karstens
W2.32
w.f.karstens@saxion.nl

Kom verder. Saxion.


Kom verder. Saxion.

Today’s topics
• Written exam information
• Example exam questions & answers

2 Programming 2 2019-2020
Kom verder. Saxion.

Final grade
Final grade = rounded written exam grade

But only if you have signed off sufficient practical


assignments (at least one each week).

Example:
Sufficient amount of practical assignments.
Written exam grade = 6.6 (= 7)
Final grade = 7

3 Programming 2 2019-2020
Kom verder. Saxion.

Written exam
It is always possible to do the written exam!

Even if you do not have signed off sufficient practical


assignments! In that case you can retake the practical
assignments in week 3.10. An announcement will be
made on Blackboard for the time/location and how
you can sign up for the retake.

4 Programming 2 2019-2020
Kom verder. Saxion.

Written exam topics


4 open questions
(total 100 points / 25 points each):

• 25 points: Functions
• 25 points: Binary numeral system & operators
• 25 points: Pointers
• 25 points: Characters and strings

5 Programming 2 2019-2020
Kom verder. Saxion.

Written exam
• The exam will be on paper.
• Duration is 2 hours (+ 30 minutes for
students entitled to special exam facilities).
• Only a pen (or pencil) is allowed to use during
the written exam. You are not allowed to use
any tools, books, notes or your laptop.
• An example exam available on Blackboard.
• A reference sheet (see Blackboard at General
information) will be added to the question
papers.
6 Programming 2 2019-2020
Kom verder. Saxion.

Today’s topics
• Written exam information
• Example exam questions & answers

7 Programming 2 2019-2020
Kom verder. Saxion.

Open question 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;
unsigned int nr_passed_students = 0;
float average;

8 Programming 2 2019-2020
Kom verder. Saxion.

Open question 1
Given the following program:

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;

9 Programming 2 2019-2020
Kom verder. Saxion.

Open question 1
Given the following program:

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


{
total = total + grades[index];
}

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

10 Programming 2 2019-2020
Kom verder. Saxion.

Open question 1
Given the following program:

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;
}
11 Programming 2 2019-2020
Kom verder. Saxion.

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

12 Programming 2 2019-2020
Kom verder. Saxion.

Open question 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;
unsigned int nr_passed_students = 0;
float average;

13 Programming 2 2019-2020
Kom verder. Saxion.

Open question 1
Given the following program:

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


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

if (entered_value < 0.0)


{
The user enters maximum
break;
}
MAX_GRADES grades in the
else
console and can stop by
{
entering a negative value.
grades[index] = entered_value;
}
}

nr_grades = index;

14 Programming 2 2019-2020
Kom verder. Saxion.

Open question 1
Given the following program:

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


{
total = total + grades[index];
}

if (nr_grades > 0)
{ The average of the entered
average = total / nr_grades; grades is calculated.
}
else
{
average = 0.0;
}

15 Programming 2 2019-2020
Kom verder. Saxion.

Open question 1
Given the following program:

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


{
if (grades[index] >= 5.5) The number of
{ passed students
nr_passed_students++; is counted.
}
}

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


The entered
{
printf("Grade %d: %f\n", index + 1, grades[index]);
grades, the
}
average and the
number of
if (nr_grades > 0) printf("Average = %f\n", average); passed students
are printed in the
printf("Number of passed students: %d\n", nr_passed_students); console.

return 0;
}
16 Programming 2 2019-2020
Kom verder. Saxion.

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

• The user enters maximum MAX_GRADES grades in the console and


can stop by entering a negative value.

• The average of the entered grades is calculated.

• The number of passed students is counted.

• The entered grades, the average and the number of passed students
are printed in the console.

17 Programming 2 2019-2020
Kom verder. Saxion.

Open question 1
b) The parts identified in a) will be written in functions of a new
program. 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.

The user enters maximum MAX_GRADES grades in the console and can
stop by entering a negative value.
unsigned int enter_grades(void);

• return value (unsigned int) : The number of grades entered.

18 Programming 2 2019-2020
Kom verder. Saxion.

Open question 1
b) The parts identified in a) will be written in functions of a new
program. 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.

The average of the entered grades is calculated.


float calculate_average(unsigned int nr_grades);

• unsigned int nr_grades : The number of grades entered.


• return value (float) : The average of the entered grades.

19 Programming 2 2019-2020
Kom verder. Saxion.

Open question 1
b) The parts identified in a) will be written in functions of a new
program. 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.

The number of passed students is counted.


unsigned int count_nr_passes_students(unsigned int nr_grades);

• unsigned int nr_grades : The number of grades entered.


• return value (unsigned int) : The number of passed students.

20 Programming 2 2019-2020
Kom verder. Saxion.

Open question 1
b) The parts identified in a) will be written in functions of a new
program. 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.

The entered grades, the average and the number of passed students
are printed in the console.
void print_results(
unsigned int nr_grades,
float average,
unsigned int nr_passed_students);

• unsigned int nr_grades : The number of grades entered.


• float average: The average of the entered grades.
• unsigned int nr_passed_students : The number of passed students.

21 Programming 2 2019-2020
Kom verder. Saxion.

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

#define MAX_GRADES 10
float grades[MAX_GRADES] = {0.0};

int main()
{
unsigned int nr_grades;
unsigned int nr_passed_students;
float average;

nr_grades = enter_grades();
average = calculate_average(nr_grades);
nr_passed_students = count_nr_passes_students(nr_grades);
print_results(nr_grades, average, nr_passed_students);

return 0;
}

22 Programming 2 2019-2020
Kom verder. Saxion.

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

unsigned int num = 20;

if (num == 0)
{
printf("Number is zero. No bits set to 1.\n");
}
else
{
int index;
for (index = -1; num > 0; index++)
{
num = num >> 1;
}

printf("MSB position: %d\n", index);


}
23 … Programming 2 2019-2020
Kom verder. Saxion.

Open question 2 (alternative)


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

unsigned int num = 20;

if (num == 0)
{
printf("Number is zero. No bits set to 1.\n");
}
else
{
int index, msb_pos = 0;
for (index = 0; index < sizeof(num) * 8; index++)
{
if((num >> index) & 1) msb_pos = index;
}

printf("MSB position: %d\n", msb_pos);


}
24 … Programming 2 2019-2020
Kom verder. Saxion.

Open question 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
given integer variables.

void swap(int * p1, int * p2)


{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}


int x = 1;
int y = 2;

swap(&x, &y);

25 Programming 2 2019-2020
Kom verder. Saxion.

Open question 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.

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


int index;

for (index = 0; text[index] != '\0'; index++)


{
if (text[index] >= 'a' && text[index] <= 'z')
{
text[index] = text[index] - 'a' + 'A';
}
}

26 Programming 2 2019-2020
Kom verder. Saxion.

Open question 4 (alternative)


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.

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


int index;

for (index = 0; *(text + index) != '\0'; index++)


{
if (*(text + index) >= 'a' && *(text + index) <= 'z')
{
*(text + index) = *(text + index) - 'a' + 'A';
}
}

27 Programming 2 2019-2020
Kom verder. Saxion.

Open question 4 (alternative)


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.

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


int index;
int text_length = strlen(text);

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


{
if (text[index] >= 'a' && text[index] <= 'z')
{
text[index] = text[index] - 'a' + 'A';
}
}
28 Programming 2 2019-2020
Kom verder. Saxion.

Good luck with the exam!

29 Programming 2 2019-2020
Kom verder. Saxion.

Questions

30 Programming 2 2019-2020

You might also like