Professional Documents
Culture Documents
GET theValue
total = 0
DO WHILE (theValue > 0)
total = total + theValue
GET theValue
LOOP
Display total
a[3] = {1, 3, 5 };
b = 23;
int
*p = a, *q = &b;
p++;
(*p)++;
*q = p[1];
for (int i = 0; i < 3; i++)
cout << a[i];
cout << endl;
cout << b << endl;
(3 marks)
Total 20 marks
Question 2
(a) Explain the meaning of the following function prototype
void foo (int a, int& b, string c);
(3 marks)
(b) Explain the difference between the concepts of call by value and call by reference.
(4 marks)
(c)
i) When the program below is run without any changes to it, what is the output of
the program?
#include <iostream>
using namespace std;
void exchange(int, int);
int main()
{
int first = 12, second = 32;
exchange ( first, second); // call the function
cout << The values are << first << << second;
return 0;
}
// function to exchange values stored in two variable
void exchange (int x, int y)
{
int tmp = x;
x = y;
y = tmp;
}
(3 marks)
ii) Rewrite the function to ensure that the two values are exchanged. State the
modified call to the exchange function.
(4 marks)
(d) A C++ function, integerList(), has been developed to list the value of the elements of
an array of integers. Both the array and a counter, specifying how many elements are
in the array are passed to the function, integerList(), which displays all the values in
the following manner:
Array element 1 has a value of 123
Array element 2 has a value of 34
3
The following program has been designed to test integerList(). Line numbers have
been given for your convenience.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
#include <iostream>
using namespace std;
//function prototype is placed here
int main()
{
int Count = 4;
int InArray[4] = {12,5,-7,9};
integerList(Inarray, Count);
cout << The array is displayed" << endl;
return 0;
}
You are required to write a statement replacing line 04 as the function prototype
for ListArray() and to write the definition for integerList() function.
(6 marks)
Total 20 marks
Question 3
(a) Name the three basic control structures and briefly describe each of them, illustrating
your answer using flowcharts. Give examples of C++ statement that implement the
control structures
(4 marks)
(b)
i) Convert the following code fragment to a switch statement.
if (x > 0 && x <= 3)
y += 1;
else if (x == 4)
y += 2;
else if (x > 4 && x <= 6)
y += 3;
else
y += 4;
(4 marks)
Question 4
(a) Suppose the file test.dat contains 3 integers separated by spaces. For example:
21 35 60
Write C++ code to open the file, read in 3 integers from the file, and computes then
displays the average of the three integers. For the above file, the output of your
program should be:
The averages of 21, 35, 60 is: 38.67
(7 marks)
(b) Complete the C++ program below that prompts the user for a name in first name,
surname order. Your program should then print, as output, the surname, followed by a
comma, followed by the first name.
For example:
Type first name followed by last name: Brown Ondego
output: Ondego, Brown
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout<<Type first name followed by last name:;
//incomplete code
return 0;
}
The following string functions would be useful
Function
Purpose
find()
returns the first position of the specified substring in the string
usage: str.find(string)
substr()
returns the substring which starts at position start and contains count
characters.
usage: str.substr(x, y) or str.substr(x)
(8 marks)
(c)
i) Explain what is meant by the terms global and local identifiers with regard to
access.
(2 marks)
ii) What is the output of this program?
#include <iostream>
using namespace std;
int x=1;
int main()
{
cout << x << endl;
func(10,20);
cout << x << endl;
int x=3;
func(10,20);
cout << x << endl;
return 0 ;
}
void func(int a, int b)
{
x=a+b;
}
(3 marks)
Total 20 marks
Question 5
(a) Explain what you understand by an algorithm. Briefly describe the properties of an
algorithm.
(3 marks)
(b) Daima Bank charges a bank fee to a customers account every time they deposit
money. The fee is based on the amount deposited, and calculated using the following
rates:
Deposit amount
less than or equal to
1,000/=
more than 1,000/= but less
than or equal to 10,000/=
more than 10,000 /=
You are contracted to write a C++ program to help the bank in its tasks of
determining the bank fee payable for each account held in the bank.