1. CPPH501 – PAPER A. (2019 S1)
1
YEAR:
Student Number
Surname & Initials
2019
SEMESTER:
ASSESSMENT:
1
A
SUBJECT NAME: Computer Programming 1
SUBJECT CODE: CPPH501
QUALIFICATION(S): D3IP13 /INDUSTRIAL PHYSICS
PAPER DESCRIPTION: COMPUTER
BASED
DURATION: 3 HOURS PAPER: ONLY
SPECIAL REQUIREMENTS
NONE
NON-PROGRAMMABLE POCKET CALCULATOR
SCIENTIFIC CALCULATOR
COMPUTER ANSWER SHEET
GRAPH PAPER
DRAWING INSTRUMENTS
OTHER: COMPUTER
INSTRUCTIONS TO CANDIDATES: ANSWER ALL QUESTIONS
THIS TEST IS TO BE ANSWERED ON https://mytutor.tut.ac.za
WHEN YOU ARE DONE. GIVE YOUR ANSWER SCRIPT BACK TO THE
INVIGILATOR AND SUBMIT YOUR FILE.
TOTAL NUMBER OF PAGES INCLUDING COVER PAGE: 12
TOTAL NUMBER OF ANNEXURES: 0
EXAMINER: W BHEBE FULL MARKS: 96
MODERATOR: A KHOZA TOTAL MARKS: 96
STUDENT TOTAL: ___
STUDENT %: ___
Tshwane Universit)
of Technology
2. CPPH501 – PAPER A. (2019 S1)
2
QUESTION 1 [10]
STATEMENT TRUE FALSE
Theory
1.1 The float and double data types in C++ store real numbers.
1.2 The condition in the selection structure specifies the decision you are
making and is phrased so that it results in either a true or false answer
only.
1.3 The repetition structure is used to repeatedly process one or more
program instructions until some condition is met, at which time the
structure ends.
1.4 A function header in a C++ program does not end with a semicolon.
1.5 You cannot include actual arguments when you call a void function.
1.6 A repetition structure can only include one instruction.
1.7 To use the predefined function sqrt(), the program must include
the header file cmath.
1.8 The output of the statement:
cout << pow(3.0, 2.0) + 5 << endl; is 14.
1.9 The following is an example of a valid function prototype:
void calc(double, double, double &, double &);
1.10 The built-in srand function is an example of a value-returning
function.
3. CPPH501 – PAPER A. (2019 S1)
3
QUESTION 2 [10]
In each of the following statements, choose the only correct answer.
2.1 The ____ structure makes a decision and then takes an appropriate action based on
that decision
a. selection
b. case
c. repetition
d. iteration
e. None of the above
2.2 ____ is an example of a syntax error.
a. cout<<’Hello’;
b. cin>>raiseRate;
c. cout<<”New Pay :”<<newPay<<endl;
d. average =number1 +number2/2;
e. None of the above
2.3 The ____ statement tells the computer to leave the switch statement at that point.
a. break
b. continue
c. stop
d. case
e. None of the above
2.4 If the default clause is not the last clause in the switch statement, you will need to
Include a ____ statement at the end of the clause to stop the computer from
processing the instructions in the next case clause.
.
a. continue
b. break
c. stop
d. switch
e. None of the above
2.5 A loop that processes its instructions indefinitely is referred to as a(n) ____ loop.
a. infinite
b. non sentinel
c. accumulating
d. incorrect
e. None of the above
4. CPPH501 – PAPER A. (2019 S1)
4
2.6 Every C++ program contains at least one function: ____.
a. main()
b. pow()
c. return()
d. rand()
e. None of the above
2.7 In standard C++, the random number generator is a built-in function named ____.
a. rand()
b. random()
c. rnd()
d. rndm()
e. None of the above
2.8 When a function definition appears below the main () function, you must enter a
function ____ above the main() function.
a. argument
b. parameter
c. prototype
d. declaration
e. None of the above
2.9 To pass a variable by reference in C++, you include a (n) ____ before the name of
the corresponding formal parameter in the receiving function’s header.
a. *
b. @
c. #
d. &
e. None of the above
2.10 ____ is an example of a valid function call.
a. void calc(double, double, double &, double &);
b. calc(salary, raiseRate, raise, newSalary);
c. void calc(double current, double rate, double &increase, double &pay)
d. calc(salary, raiseRate, &raise, &newSalary);
e. None of the above
5. CPPH501 – PAPER A. (2019 S1)
5
QUESTION 2 (MULTIPLE CHOICE)
2.1 a b c d e
2.2 a b c d e
2.3 a b c d e
2.4 a b c d e
2.5 a b c d e
2.6 a b c d e
2.7 a b c d e
2.8 a b c d e
2.9 a b c d e
2.10 a b c d e
6. CPPH501 – PAPER A. (2019 S1)
6
QUESTION 3 [21]
3.1 What is an escape character? Give an example. [2]
3.2 Propose an appropriate prototype of a function that will return the higher of any two
numbers passed as argument to the function [4]
3.3. Write C++ statements for the following: [3]
𝑝𝑝 = �𝑎𝑎2 + 𝑏𝑏2
7. CPPH501 – PAPER A. (2019 S1)
7
3.4. Write C++ statements for the following: [3]
𝑠𝑠𝑠𝑠𝑠𝑠 =
𝑎𝑎(𝑟𝑟𝑛𝑛
− 1)
𝑟𝑟 − 1
3.5 Explain in your own words, the term ‘modularity’ [3]
8. CPPH501 – PAPER A. (2019 S1)
8
3.6 Study the following code and rewrite the program using a “for” loop [6]
Write your answer in the space provided below
int number = 1;
do
{
cout << number <<
endl; number++;
} while (number <= 10);
9. CPPH501 – PAPER A. (2019 S1)
9
QUESTION 4 [16]
Create a program that displays a series of the first 10 Fibonacci numbers (1, 1, 2, 3, 5, 8,
13, 21, 34, and 55). Notice that, beginning with the third number in the series, each
Fibonacci number is the sum of the prior two numbers. In other words, 2 is the sum of 1
plus 1, 3 is the sum of 1 plus 2, 5 is the sum of 2 plus 3, and so on. Write two versions of
the code: one using the for statement and the other using the while statement. Enter
the C++ instructions into a source file named Question5.cpp. Add appropriate comments
and any additional instructions required to execute your program. Note: the Fibonacci
numbers should appear twice on the screen. Refer to figure 4.1 for sample output.
Figure 4.1
QUESTION 5 [6]
Write a C++ for loop that will produce the output in figure 5.1. Include appropriate
comments and any additional instructions required to execute your program.
Figure 5.1
I C:Usersraphirits.TUTD~sktopSickT~st2D~bugSickT~st2.~x~
i s playing t e the FIRST 10 i onacci
umbers us ing [for s tatement]
i s playing the the FIRST 10 Fibonacci
umbers us ing [while s t a tement]
any key to continue . . . _
Squat'e
4
16
36
64
100
any key
Cube
8
64
216
512
1000
to continue
V
10. CPPH501 – PAPER A. (2019 S1)
10
Question 6 [23]
Create a program for the sales manager at Computer Haven, a small business that offers
motivational seminars to local companies. Table 6-1 shows the charge for attending a
seminar. Notice that the charge per person depends on the number of people the company
registers. For example, the cost for four registrants is R5 761.32; the cost for two registrants
is R4 321.0. The program should allow the sales manager to enter the number of registrants
for as many companies as needed. When the sales manager has finished entering the data,
the program should calculate and display the total number of people registered, the total
charge for those registrants, and the average charge per registrant. For example, if one
company registers four people and another company registers two people, the total number
of people registered is six, the total charge is R10 082.32, and the average charge per
registrant is R1680.38.
Number of people a
company registers
Charge per person (R)
1 – 3 2160.50
4 – 9 1440.33
10 or more 1296.30
Table 6.1
Include necessary statements to compile and run your program. See figure 6.1 for sample
output.
11. CPPH501 – PAPER A. (2019 S1)
11
Figure 5-1
QUESTION 7 [10]
Write, compile and run a C++ program to input the following values into an array
named velocity: 10.95, 16.32, 12.14, 8.33, 45.20, 36.45, 7.89, 12.35 and 11.54.
After your data has been entered have your program display the values.
first coApany (negative nuAber to stop the prograA): 4
registered by the next coApany (negative nuAber to stop the prograA): 2
registered by the next coApany (negative nuAber to stop the prograA): -5
otal nuAber of registrants: 6
otal charge for all registrants: R10082.32
uerage charge per registrant: R1680.39
registered by the next COApany (negative
registered by the next COApany (negative
registered by the next COApany (negative
registered by the next COApany (negative
registered by the next COApany (negative
registered by the next COApany (negative
registered by the next COApany (negative
registered by the next COApany (negative
registered by the next COApany (negative
otal nuAber of registrants: 50
otal charge for all registrants: R?561?.39
verage charge per registrant: R1512.35
nm11ber to stop the pl"Ogl"aA): 8
nm11ber to stop the pl"Ogl"aA): 3
nm11ber to stop the pl"Ogl"aA): 1
nm11ber to stop the pl"Ogl"aA): 3
nm11ber to stop the pl"Ogl"aA): 4
nm11ber to stop the pl"Ogl"aA): ?
nm11ber to stop the pl"Ogl"aA): 10
nm11ber to stop the pl"Ogl"aA): 9
nm11ber to stop the pl"Ogl"aA): -1
y
12. CPPH501 – PAPER A. (2019 S1)
12
INSTRUCTIONS TO STUDENTS
- All codes must be copied into a single file (MS word file, or any other format
recommended by the lecturer)
- The file name must match your student number
- Student name and student number must be written at the top of the submitted file!!
- All practical work must be submitted on myTutor
- Make sure the lecturer or invigilator has collected your project before leaving the
venue!