CSC128 CSC415
CSC128 CSC415
CONFIDENTIAL
COURSE
COURSE CODE
CSC128/415
EXAMINATION
OCTOBER 2009
TIME
3 HOURS
INSTRUCTIONS TO CANDIDATES
1.
2.
Answer ALL questions from all THREE (3) parts in the Answer Booklet. Start each answer
for PART B and PART C on a new page.
3.
Do not bring any material into the examination room unless permission is given by the
invigilator.
4.
Please check to make sure that this examination pack consists of:
i)
ii)
CONFIDENTIAL
CONFIDENTIAL
CS/OCT 2009/CSC128/415
Flowchart and pseudocode can be used to design the solution to the problem.
Debugging is testing a program and correcting errors in the program.
A syntax error is a violation of the rules of a programming language.
The purpose of the program maintenance is to ensure that current programs are
operating error free, effectively and efficiently.
A. I, III
B. II, IV
C . I, II, III
D. I, II, III, IV
2. What error is caused by a correctly written statement but used at the wrong place?
A.
B.
C.
D.
Human
Logic
Syntax
Typing
A.
B.
C.
D.
all
contingency
default
other
CONFIDENTIAL
CS/OCT 2009/CSC128/415
Sequential, selection and repetition are the types of program control structures.
A loop control variable must be initialized.
A loop stops executing its body when the loop repetition condition is true.
An infinite loop is a loop that executes forever.
6. How many lines of output will be displayed by the following program segment?
for
A.
B.
C.
D.
(i = 0; i < 2; i++)
f o r ( j = 0; j < 3; j++)
c o u t " ********* \ n .
2
3
6
12
7. Which of the following statements would correctly assign the entire array declared below
toO?
int arr[5];
A. for (int m = 1;
arr[m] = 0;
B. for (int m = 0;
arr[m] = 0;
C. for (int m = 1;
arr[m] = 0;
D. for (int m = 0;
arr[m] = 0;
m <= 5; m++)
m <= 5; m++)
m < 5; m++)
m < 5; m++)
A.
B.
C.
D.
3
5
10
13
CONFIDENTIAL
CONFIDENTIAL
CS/OCT 2009/CSC128/415
float);
Which of the following function calls are NOT valid when the declaration below is given?
float a, b, answer;
A.
B.
C.
D.
answer = kira(a, b) ;
if (kira(a, b) == a) b = a;
cin >> kira(a, b ) ;
cout kira(a, b ) ;
endl;
void fl(int& k)
{
k = k * k;
}
int f2(int k)
{
k = k * k;
return k;
}
A.
81
B.
C.
D.
27
9
3
CONFIDENTIAL
CONFIDENTIAL
CS/OCT 2009/CSC128/415
QUESTION 1
a) Given the following declarations:
double x = 5.0, y = 9.0,
i n t w = 3 , v = 6;
z = 2.0;
x + y / z
z / v * w
w + x %z
v / x * (y - 4)
(x + y) / z + w
(5 marks)
i
..
II.
y V
z3 2 y-\
x
z
>> + 3
(3 marks)
CONFIDENTIAL
CONFIDENTIAL
CS/OCT 2009/CSC128/415
QUESTION 2
a) Write a C++ program segment to do the following:
Display the character ' A ', ' B', ' c ' or ' D ' when the value of integer num is 2, 4, 6
or 8, respectively. Otherwise, display the error message.
(4 marks)
b) Write a complete C++ program for the following flowchart:
Start
Input
salary, year
yes
no
bonus =
1.5 x salary
bonus =
1.2 x salary
Display
bonus
End
(5 marks)
CONFIDENTIAL
CONFIDENTIAL
CS/OCT 2009/CSC128/415
QUESTION 3
a) Rewrite the following program segment using a w h i l e statement:
float marks, totMarks = 0, average;
int i = 0;
for (cin >> marks; marks >= 0; cin >> marks)
{
totMarks = totMarks + marks;
cout << (i + 1) ": Marks = " marks
endl;
i + +;
}
average = totMarks / i;
cout << "Average marks = "
average
endl;
(4 marks)
b) What is the output of the above program segment if the input is:
90 80 100 - 1
(2 marks)
c) The program below should accept THREE nonnegative integer values less than 1000
from the user. Then, the program should print the biggest and the lowest number of the
inputs. However, the program has some logic errors. Find and fix the errors.
void main()
{
int num, smallest = 1000, biggest = 0, counter = 0;
do
{
cout << "Enter an integer smaller than 1000: ";
cin >> num;
if (num > smallest)
biggest = num;
else
if (num < biggest)
smallest = num;
counter++;
}
while (num >= 0 && num < 1000);
cout << "The biggest is " << biggest endl;
cout << "The smallest is " smallest << endl;
(6 marks)
Hak Cipta Universiti Teknologi MARA
CONFIDENTIAL
CS/OCT 2009/CSC128/415
CONFIDENTIAL
QUESTION 4
a) What is the difference between a function prototype and function definition?
(2 marks)
b) Consider the following C++ program and answer the questions that follow:
void t r i c k y ( i n t & ,
void main()
int&);
i n t n l , n2, n 3 , n4, x;
cout "Enter 4 integer numbers: ";
cin nl n2 n3 n4;
do
{
x = 0;
if (nl > n2)
tricky(nl, n2) ;
x = 1;
if (n2 > n3)
tricky(n2, n3);
x = 1;
if (n3 > n4)
tricky(n3, n4);
x = 1;
while (x);
cout nl
}
void
{
x
y
x
}
' '
n2
'
n3
' '
n4
endl;
tricky(int& x, int& y)
= x - y;
= x + y;
= y - x;
Trace and write the output with numbers 4, 7, 1 and 2 as the input,
i. What is the purpose of function t r i c k y () ?
ii. What does the above program do?
(8 marks)
CONFIDENTIAL
CONFIDENTIAL
CS/OCT 2009/CSC128/415
QUESTION 5
a) num is an integer array of ten elements. Write C++ statements to do the following:
i. Declare num.
ii. Read all elements of num.
iii. Display all positive integers in num.
(5 marks)
b) Trace and write the output for the following C++ program.
void main()
{
int res[20] = {1, 2, 1, 4, 3, 5, 4, 2, 3, 5,
1, 1, 3, 3, 1, 5, 3, 3, 2, 2};
int freq[6];
for (int i = 0; i < 20; i++)
for (int rate = 1; rate < 6; rate++)
if (res [i] == rate)
freq[rate]++;
cout << "Rating
freq[rate]
endl;
(6 marks)
CONFIDENTIAL
CONFIDENTIAL
10
CS/OCT 2009/CSC128/415
QUESTION 1
The table below shows the Glow Water Theme Park ticketing price:
Package
Package Code
Child
Family Package
40.00
21.00
Fun Package
23.00
14.00
Splash Package
38.00
18.00
CONFIDENTIAL
CONFIDENTIAL
11
CS/OCT 2009/CSC128/415
QUESTION 2
Write a C++ program to do the following:
a) Define a function s p l i t () that receives a four-digit positive integer. The function will
split the four-digit integer into four single digits and store the digits into an array of
integer. The function then returns the array.
(4 V2 marks)
b) Define a function s h i f t () that receives an array and its size. The function will shift all
the elements of the array to the left. The last element will take the value of the first
element.
(3 1/4 marks)
c) Define main program that reads a four-digit integer. By calling the functions defined in a)
and b) above, the program will split the four digits into an array and display the elements
of the array every time the elements have shifted to the left until we get the original array.
The program should also check whether the integer entered is positive and not more
than four digits. Otherwise, the program will print the appropriate message and stop.
For example: If the input is 12 34, the array elements are 1, 2, 3 and 4 respectively. The
program then displays the following:
1234
2341
3412
4123
(7 marks)
CONFIDENTIAL