COS1511 Oct 2022
COS1511 Oct 2022
75
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.
[TURN OVER]
Open Rubric
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;
}
What will be the output of the following statement executed in the main function?
[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.
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++;
}
QUESTION 3 8 MARKS
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.
#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.
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.
[TURN OVER]
5 COS1511
OCT/NOV 2022
Assume that countTwo, countFour and countMore have been initialised already and
that a value has been input and validated for nrWheels.
QUESTION 6 9 MARKS
6.1 Write a for-statement to display the numbers 1 through 10 on the screen. (2)
6.2 Write a while-statement to display the numbers 1 through 10 on the screen. (3)
6.3 The code below should display the numbers 1, 2, 3 and 4 on the screen. However, the code
is not working correctly. Correct the errors in the code. (2)
1 int num = 1;
2 while (num < 4)
3 cout << num << endl;
4 // endwhile
6.4 The code below should display each salesperson's commission. The commission is calculated
by multiplying the salesperson's sales by 10%. The code is not working correctly. Correct the
errors in the code. (2)
QUESTION 7 6 MARKS
The Fruit Packers keep record of the number of crates of fruit that are packed for each day for
a whole year (365 days). These 365 values are stored in an int array called crates. You have to write
a function, called calcAverage to determine the average number of crates packed per day for the
year.
[TURN OVER]
6 COS1511
OCT/NOV 2022
values have been assigned already to all the elements of the array
the function is called in the main function as follows:
average = calcAverage(crates);
QUESTION 8 8 MARKS
8.1 Write a function header for the function check that has two parameters. The first parameter
should be an integer value and the second parameter a floating point value. The function
returns no value. (1)
8.2 Write a function header for the function mult that has two floating point numbers as
parameters and returns the result of multiplying them. (1)
8.3 Write a function header for the function time that inputs seconds, minutes and hours and
returns them as parameters to its calling function. (1)
8.4 Write a function header for the function countLets that returns the number of occurrences
of a character in a string, both provided as parameters. (1)
8.5 Suppose the following declarations appear in the main function of a C++ program:
int number;
float cost, markup, discount;
Also, suppose the following calling statement appears in the main function:
8.6 Suppose the following declarations appear in the main function of a C++ program:
string dayOfWeek;
int productCode, number;
float discount;
give the correct calling statement of the function calcDiscount in the main function.
(2)
[TURN OVER]
7 COS1511
OCT/NOV 2022
QUESTION 9 8 MARKS
Bookworm bookstores announced a competition running over four weeks for their three branches.
The branch with the highest sales per week will receive a surprise for that week. You as a
programmer are requested to write a program to keep record of the number of books sold per week in
each of the three branches over this period of four weeks. The (incomplete) program below inputs the
respective number of books sold and stores it in a two-dimensional array called sales, with four rows
and three columns. The program then displays for each week, the highest number of books sold.
Here is an example of the input data for the program:
Use the declarations in the (incomplete) program below and do the following:
9.2 Assume array sales have been initialised. Write a program fragment to determine and
display the highest sales per week. (6)
#include <iostream>
using namespace std;
const int NUM_WEEKS = 4;
const int NAME = 3;
int main()
{
int highest;
return 0;
[TURN OVER]
8 COS1511
OCT/NOV 2022
}
[TURN OVER]
9 COS1511
OCT/NOV 2022
QUESTION 10 6 MARKS
struct Product
{
string name;
float weight;
float price;
}
10.1 Given this structure type definition above, suppose the following declaration appear in
the main function of a C++ program
Product p1;
Write a cout statement that will display all the fields of the Product p1. (2)
QUESTION 11 7 MARKS
In this question you have to write a function that receives as input a person's first names and
surname, and then display just the initials. For example, if the input is John Peter Doe, the initials
JPD must be displayed.
Member functions of the string class to manipulate the values of string objects are provided on
the next page.
[TURN OVER]
10 COS1511
OCT/NOV 2016
© UNISA
2022