OCT/NOV 2022
75
This examination question paper consists of 9 pages out of 75 marks.
INSTRUCTIONS:
1. Answer all the questions on a Word document/by hand and convert to PDF.
2. Number your answers and label your rough work clearly.
4. Marks are awarded for part of an answer, so do whatever you are able to in each question.
ALL THE BEST!
[TURN OVER]
, 2 COS1511
OCT/NOV 2022
QUESTION 1 6 MARKS
1.1 Consider the C++ code segment below. What value will newval have after this code has
been executed? (2)
int var1 = 4;
int var2 = 10;
int newval = 0;
if (var1 * 2 >= var2)
newval = 5 + 2 * var2;
else if (var1 < var2)
newval = var2 - var1 * 2;
else
newval = var1;
1.2 Suppose the input value for a is 5. What is the value of a after the following C++ code has
been executed? (2)
int a;
cin >> a;
switch (a)
{
case 1: a += 3;
case 3: a = a * 3; break;
case 5: a = ++a + 10;
case 6: a /= 2;
default: a = a + 1;
}
1.3 Consider the following C++ code segment: (2)
int findValue(int numberP)
{
int count = 0;
int value = 20;
while (count < numberP)
{
value += count;
count ++;
}
return value;
}
What will be the output of the following statement executed in the main function?
cout << findValue(3);
[TURN OVER]
, 3 COS1511
OCT/NOV 2022
QUESTION 2 4 MARKS
In Questions 2.1 and 2.2 you have to write down what the purpose of the segment of code is. Look at
the following example before answering the questions:
int a,b,c;
cin >> a >> b >> c;
cout << c + b + a;
The purpose of the above code segment is to input three integer values and display their sum.
Now answer questions 2.1 and 2.2 below:
2.1 Assume that s and n have been declared as integers. Explain in words what the purpose of
the following segment of code is: (2)
int s = 0;
int n = 0;
while (n <= 5)
{
s = s + n;
n++;
}
2.2 Explain the purpose of the following segment of code: (2)
int numbers[ ] = {11, 0, 15, 0, 16, 23};
int c = 0;
for (int i = 0; i <= 5; i++)
if (numbers[i] != 0)
c += 1;
QUESTION 3 8 MARKS
Consider the following C++ code segment below.
1 int result(int valueP)
2 {
3 int a = 2;
4 int count = 0;
5 while (count < valueP)
6 {
7 a += count + a / 2;
8 count += 2;
9 }
10 return a;
11 }
Demonstrate the execution and output of the program by drawing a variable diagram that traces each
line of code if the value of value P is 6. You are required to draw a variable diagram to illustrate
what the code does.
[TURN OVER]
, 4 COS1511
OCT/NOV 2022
QUESTION 4 8 MARKS
Karlton Learning wants a program that displays the amount of money a company owes for a seminar.
The fee per person is based on the number of people the company registers. For example, if the
company registers 7 people then the amount owed is R 560.00. If the user enters a number that is less
than or equal to 0, the program should display an appropriate error message.
Number of registrants Fee per person
1 through 4 R100.00
5 through 10 R 80.00
11 or more R 60.00
Complete the program below.
#include <iostream>
using namespace std;
int main()
{
// Question 4.1 (2)
// Declare a variable to hold the number of registrants and a
// variable to hold the amount a company owes for a seminar.
// Question 4.2 (2)
// Write statements to input the number of registrants. If the
// user enters a number that is less than or equal to 0, the
// program should display an appropriate error message.
// Question 4.3 (4)
// Write statements to calculate and display the amount of money
// a company owes for a seminar.
return 0;
}
QUESTION 5 5 MARKS
In order to plan road maintenance, the department of Road Works request that road usage must be
determined. This is done by counting the number of vehicles using the road with two wheels, four
wheels and more than four wheels respectively. Write down ONLY the necessary switch statement
to count the number of vehicles with two wheels, four wheels and more than four wheels. Do NOT write
a complete program.
Use the following variables:
int nrWheels; // number of wheels of a vehicle using the road
int countTwo, countFour, countMore; // the counters for the
// number of vehicles with
// two, four and more than
// four wheels respectively
[TURN OVER]
75
This examination question paper consists of 9 pages out of 75 marks.
INSTRUCTIONS:
1. Answer all the questions on a Word document/by hand and convert to PDF.
2. Number your answers and label your rough work clearly.
4. Marks are awarded for part of an answer, so do whatever you are able to in each question.
ALL THE BEST!
[TURN OVER]
, 2 COS1511
OCT/NOV 2022
QUESTION 1 6 MARKS
1.1 Consider the C++ code segment below. What value will newval have after this code has
been executed? (2)
int var1 = 4;
int var2 = 10;
int newval = 0;
if (var1 * 2 >= var2)
newval = 5 + 2 * var2;
else if (var1 < var2)
newval = var2 - var1 * 2;
else
newval = var1;
1.2 Suppose the input value for a is 5. What is the value of a after the following C++ code has
been executed? (2)
int a;
cin >> a;
switch (a)
{
case 1: a += 3;
case 3: a = a * 3; break;
case 5: a = ++a + 10;
case 6: a /= 2;
default: a = a + 1;
}
1.3 Consider the following C++ code segment: (2)
int findValue(int numberP)
{
int count = 0;
int value = 20;
while (count < numberP)
{
value += count;
count ++;
}
return value;
}
What will be the output of the following statement executed in the main function?
cout << findValue(3);
[TURN OVER]
, 3 COS1511
OCT/NOV 2022
QUESTION 2 4 MARKS
In Questions 2.1 and 2.2 you have to write down what the purpose of the segment of code is. Look at
the following example before answering the questions:
int a,b,c;
cin >> a >> b >> c;
cout << c + b + a;
The purpose of the above code segment is to input three integer values and display their sum.
Now answer questions 2.1 and 2.2 below:
2.1 Assume that s and n have been declared as integers. Explain in words what the purpose of
the following segment of code is: (2)
int s = 0;
int n = 0;
while (n <= 5)
{
s = s + n;
n++;
}
2.2 Explain the purpose of the following segment of code: (2)
int numbers[ ] = {11, 0, 15, 0, 16, 23};
int c = 0;
for (int i = 0; i <= 5; i++)
if (numbers[i] != 0)
c += 1;
QUESTION 3 8 MARKS
Consider the following C++ code segment below.
1 int result(int valueP)
2 {
3 int a = 2;
4 int count = 0;
5 while (count < valueP)
6 {
7 a += count + a / 2;
8 count += 2;
9 }
10 return a;
11 }
Demonstrate the execution and output of the program by drawing a variable diagram that traces each
line of code if the value of value P is 6. You are required to draw a variable diagram to illustrate
what the code does.
[TURN OVER]
, 4 COS1511
OCT/NOV 2022
QUESTION 4 8 MARKS
Karlton Learning wants a program that displays the amount of money a company owes for a seminar.
The fee per person is based on the number of people the company registers. For example, if the
company registers 7 people then the amount owed is R 560.00. If the user enters a number that is less
than or equal to 0, the program should display an appropriate error message.
Number of registrants Fee per person
1 through 4 R100.00
5 through 10 R 80.00
11 or more R 60.00
Complete the program below.
#include <iostream>
using namespace std;
int main()
{
// Question 4.1 (2)
// Declare a variable to hold the number of registrants and a
// variable to hold the amount a company owes for a seminar.
// Question 4.2 (2)
// Write statements to input the number of registrants. If the
// user enters a number that is less than or equal to 0, the
// program should display an appropriate error message.
// Question 4.3 (4)
// Write statements to calculate and display the amount of money
// a company owes for a seminar.
return 0;
}
QUESTION 5 5 MARKS
In order to plan road maintenance, the department of Road Works request that road usage must be
determined. This is done by counting the number of vehicles using the road with two wheels, four
wheels and more than four wheels respectively. Write down ONLY the necessary switch statement
to count the number of vehicles with two wheels, four wheels and more than four wheels. Do NOT write
a complete program.
Use the following variables:
int nrWheels; // number of wheels of a vehicle using the road
int countTwo, countFour, countMore; // the counters for the
// number of vehicles with
// two, four and more than
// four wheels respectively
[TURN OVER]