0% found this document useful (0 votes)
6 views9 pages

COS1511 OctNov 2019

Uploaded by

j.prokopes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

COS1511 OctNov 2019

Uploaded by

j.prokopes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

This paper consists of 9 pages.

Out of 80 marks.

INSTRUCTIONS:

1. Answer all the questions in the answer book.


2. Do all rough work in the answer book as well.
3. 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 2019

QUESTION 1 5 marks

Complete the statements by providing only the missing words:

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)

1.5 insert() is a member function of the ……………. class. (1)

QUESTION 2 5 marks

State whether the following statements are true or false:

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.3 The variable declaration in C++ is optional. (1)

2.4 The assignment operator in C++ is a binary operator. (1)

2.5 The struct data structure can have members of different data types. (1)

QUESTION 3 6 marks

3.1 Declare an integer constant NUM with value 100. (1)

3.2 Concatenate two string variables name and surname to create one string in a string
variable fullName. Assume variable declarations. (1)

3.3 Consider the string initialisation:

string ticket = “Economy Class”;

[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'.

Write the correct assignment statement? (2)

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;

//the definition for function calcSum


………………………………….

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

4.3 Convert the following for loop to a while loop (3)


for (int i = 12; i >= 5; i-=1)
cout << “#” << endl;

4.4 What is the output of the following code fragment? (2)


int counter1 = 0;
int counter2 = 0;
for (int i = 0; i < 2; ++i)
{
++counter1;
for (int j = 0; j <= 4; ++j)
{
if (i == j)
++counter2;
}
}
cout << counter1 << " " << counter2;

4.5 Consider the following code for a textbook store: (6)


float price = 500; //standard price
char productStatus;

cin >> productStatus;

if(productStatus == ‘f’) //the book is sold at full price


price = price;
else if(productStatus == ‘d’) //the book is sold at a discount
price = price – (price *0.10);
else if(productStatus == ‘o’) //the book is out of stock
cout << “Out of stock” << endl;
else
cout << "Invalid product status!" << endl;

Convert the nested if statement into a switch statement.

[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.

Suede Leather Cotton


Couch 50 10 20
Chair 40 50 60
Ottoman 30 20 10

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

4. //set lowestScore to the score of the first player in the array

5. //set winningPlayer to the index of the first player

6. //search through array(declared in 8.2) to find the player with


//the lowest score
7. for ( ) //examine all players
8. {
9. //determine score for player being inspected
10. //determine lowest score and which player had lowest score
11. }
12. //print the name and score of the player with the lowest
//score
[TURN OVER]
8 COS1511
OCT/NOV 2019

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()

Example, if the string

Incy wincy spider

is given, the string

Incy wncy spder

should be returned to the main(). Write ONLY the function removeChar. (6)

#include <iostream>
#include <string>
using namespace std;

//insert function removeChar here

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;
}

You may find the following table of string functions useful.

[TURN OVER]
9 COS1511
OCT/NOV 2019

A number of string member functions to help you

StringObject.size( )

StringObject.substr(startPos, length)

StringObject.find(substring)

StringObject.find(substring, startPos)

StringObject.insert(insertPos, substring);

StringObject.erase(startPos, length);

StringObject.replace(startPos, length, substring);


where startPos, length and insertPos are of type int, and
substring is of type string.

©
UNISA
2019

You might also like