COS1511 OctNov 2019
COS1511 OctNov 2019
Out of 80 marks.
INSTRUCTIONS:
[TURN OVER]
2 COS1511
OCT/NOV 2019
QUESTION 1 5 marks
1.1 The …………….. control structure in C++ makes use of a selector expression which must be of an
ordinal data type. (1)
1.2 The ……………….. is an arithmetic operator in C++ whose operands are always integer values. (1)
1.3 The type of error that invariably causes a C++ program to crash during execution is called the
………………… error. (1)
1.4 The values provided in a function call are called the …….…………… parameters. (1)
QUESTION 2 5 marks
2.1 The statement int twoDim[][]; correctly declares a two dimensional integer array. (1)
2.2 The statement int numbers[ ] = {10}; declares a ten element array of integer values. (1)
2.5 The struct data structure can have members of different data types. (1)
QUESTION 3 6 marks
3.2 Concatenate two string variables name and surname to create one string in a string
variable fullName. Assume variable declarations. (1)
[TURN OVER]
3 COS1511
OCT/NOV 2019
Write the C++ statement that will replace the substring “Economy” in the string “Economy Class”
with “Business”, so that the value of the variable ticket becomes “Business Class” . (2)
3.4 Suppose we want to assign the value true to a bool variable pass if:
the value of the int variable exam is greater than or equal to 50 and the value of the
char variable assignmentSubmitted is'y'.
QUESTION 4 17 marks
4.1 Complete the following program by giving the full definition for the function calcSum as your
answer. The function adds the two integer values it receives. (4)
#include <iostream>
using namespace std;
void printSum(int s)
{
cout << s;
int main()
{
int a = 10, b = 5;
printSum(calcSum(a,b));
return 0;
}
4.2 Suppose the five C++ instructions below occur in a program. What will be the output displayed on
the screen? (2)
int x;
int y = 113;
y--;
x = y++;
cout << x << " " << y << endl;
[TURN OVER]
4 COS1511
OCT/NOV 2019
[TURN OVER]
5 COS1511
OCT/NOV 2019
QUESTION 5 6 marks
Write a function header for each of the following (Write ONLY the function header):
5.1 The function circle() that receives the radius of a circle as a floating point value parameter, and
calculates the area and circumference of the circle, storing the results in two reference
parameters. (2)
5.2 The function countSpaces() that returns the number of spaces in a string received as a
parameter. (2)
5.3 The function searchArray() that receives an array of integers intArray and an integer
check as its parameters. The function returns a true value if the integer check is in the array
intArray. (2)
QUESTION 6 8 marks
Draw variable diagrams for the program given below using the input 20 10 5 (8)
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 int a, b, c, temp;
6 cout<< "Enter values for a, b and c, separated by spaces: ";
7 cin >> a >> b >> c;
8 if (b > c)
9 {
10 temp = b;
11 b = c;
12 c = temp;
13 }
14 else if (a > c)
15 {
16 temp = a;
17 a = c;
18 c = temp;
19 }
20 if (a > b)
21 {
22 temp =a;
23 a = b;
24 b = temp;
[TURN OVER]
6 COS1511
OCT/NOV 2019
}
25 cout << a << ' ' << b << ' ' << c << endl;
26 return 0;
27 }
QUESTION 7 12 marks
The below table contains the stock of three types of furniture items in three different upholstery covering
material. The numbers in stock have to be stored in a two-dimensional integer array.
7.1 Write the program statements to declare an integer constant STOCK_ITEMS for the number of
different furniture items in stock, and an integer constant NR_MATERIALS for the number of
upholstery types. (2)
7.2 Write down the necessary C++ statement to declare and initialise the stockArray with the values
in the table. Use the constants declared in 7.1 (2)
7.3 For stock taking purposes, the owner wants to know how many units of furniture are in the shop.
(a) Write down the necessary C++ statements to calculate and display the total number of furniture
items in the shop. Do NOT write a complete program. Write down ONLY the required statements.
Assume the following declaration:
int totalItems = 0;
(4)
(b) Write down the necessary C++ statements to calculate and display the total number of leather
furniture items in the shop. Do NOT write a complete program. Write down ONLY the required
statements. Assume the following declaration: (4)
int leatherItems = 0;
[TURN OVER]
7 COS1511
OCT/NOV 2019
QUESTION 8 15 marks
The Great Golf Club registers the following information for each player on a specific day:
name (a string, for example “Gary Player”)
handicap (an integer, for example 10)
shots (an integer, for example 49)
8.1 Write down the declaration for a struct for storing the information associated with one player.
Give the name Player to the struct. (4)
8.2 Declare an array named players with 50 elements of type Player. (2)
8.3 Assume that the information for 50 golf players have been stored in the array declared in 8.2. The
program fragment below determines which player had the lowest score for the day. The score for
each player is determined by deducting the player’s handicap from the number of shots the player
has taken. Then the name and the score for the player with the lowest score is printed.
Now write down ONLY the necessary C++ instructions for line numbers 4, 5, 7, 9, 10 and 12 to
complete this program fragment. Write down only the line number and the instruction that should
appear next to the line number. (9)
1. int lowestScore;
2. int winningPlayer; //used within the loop to store the index of
//the player with the lowest score
3. int score; //used within the loop to store the calculated score
//for a player
QUESTION 9 6 marks
In this question the main() is given. You have to write a function removeChar that is called from the
main().
The function removeChar receives a string as parameter. The function has to do the following:
find all occurrences of “i” in the string and erase them
return the changed string to the main()
should be returned to the main(). Write ONLY the function removeChar. (6)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string sentence;
//prompt user to enter a sentence
cout << "Enter a sentence:" << endl;
getline(cin, sentence, '\n');
//output
cout << "New sentence:" << endl;
cout << removeChar(sentence) << endl;
return 0;
}
[TURN OVER]
9 COS1511
OCT/NOV 2019
StringObject.size( )
StringObject.substr(startPos, length)
StringObject.find(substring)
StringObject.find(substring, startPos)
StringObject.insert(insertPos, substring);
StringObject.erase(startPos, length);
©
UNISA
2019