You are on page 1of 8

STRATHMORE UNIVERSITY

FACULTY OF INFORMATION TECHNOLOGY


BACHELOR OF BUSINESS INFORMATION TECHNOLOGY
END OF SEMESTER EXAMINATION
BBT 1102: INTRODUCTION TO COMPUTER PROGRAMMING
TIME: 2 Hours
DATE: 21st October, 2008
INSTRUCTIONS:
ANSWER QUESTION ONE AND ANY OTHER TWO QUESTIONS
Question 1
(a) Briefly explain the following terms
i) High-level Language
ii) Object code
(2 marks)
(b) Explain the main difference between a compiler and an interpreter
(2 marks)
(c) Given the following pseudo code,
1.
2.
3.
4.
5.
6.
7.

GET theValue
total = 0
DO WHILE (theValue > 0)
total = total + theValue
GET theValue
LOOP
Display total

Desk check the algorithm using the following input values 2, 3, 5, 0, 2, -1


(4 marks)
(d) What value is assigned to the integer variable y?
i) y = 3 / 5 * 3 + 4;
ii) y = 53 % (4 * 3) + 1
(2 marks)
(e) Explain the difference between an array and a structure, giving an example of where
you would use a structure and an example of where you would use an array.
(4 marks)

(f) What is the output of the following program segments?


i)
for (int i = 0; i < 2; ++i)
{
cout << "*****";
cout << endl;
for (int j = 0; j < 2; ++j)
{
cout << "* *" << endl; //3 spaces between
}
}
cout << "*****"<<endl;
(2 marks)
ii)
int
int

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)

ii) What is the value of variable n after following C++ statements?


int n = 5, a = -1, b = 3;
switch ((--n)%4)
{
case 0:
n++; break;
case 1:
n *= a++; break;
case 2:
n +=--b; break;
case 3:
--n; break;
default:
n++; break;
}
(4 marks)
(c) The algorithm shown below reports how many of the numbers entered by the user are
even (i.e. are divisible by 2). This algorithm uses a WHILE loop.
BEGIN
evens 0
count 1
INPUT max
WHILE count <= max
INPUT number
IF number mod 2 = 0 THEN
evens evens+1
ENDIF
count count+1
ENDWHILE
DISPLAY evens
END
i) Rewrite the algorithm using a DO WHILE loop
(3 marks)
ii) Write a C++ program fragment that uses a FOR loop that accomplishes exactly
the same results.
(5 marks)
Total 20 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 /=

Bank fee charged


1 % of amount
10 /= for the first 1,000/= plus 2%
for amount above 1,000 /=
190 /= for the first 10,000 /= plus
3% for amount above 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.

The following incorrect algorithm to calculate the fee is suggested


Begin
1.
Declare variables for deposit amount, bank fee.
2.
Get deposit amount.
3.
If deposit amount 1000 Then
4.
bank fee = deposit amount x 0.01
5.
Else
6.
If deposit amount 10000 Then
7.
bank fee = 10 + deposit amount x 0.02
8.
Else
9.
bank fee = 190 + deposit amount x 0.03
10.
End-If
11.
End-If
12.
Display bank fee
End
i) Desk check the algorithm using a deposit amount of 2,000/=
(4 marks)
ii) Rewrite the algorithm making corrections. Clearly indicate any changes you
have made.
(3 marks)
iii) Convert the algorithm into a complete C++ program
(6 marks)
(c) Write a program that accepts the name of a student, and three pieces of homework
marks, and then calculates the average of the marks attained by the student. The
details of the student should be stored in a struct.
(4 marks)
Total 20 marks

You might also like