Sei sulla pagina 1di 5

NATIONAL UNIVERSITY OF LESOTHO

Department of Mathematics and Computer Science


CS 2431-Programming Fundamentals
Test 01 - Open Book
28th September 2005

Marks: 100

Time: 2 hours

Instructions:
(1)
(2)
(3)

Answer any five (5) questions. Each question carries 20 marks.


Marks are shown at the end of each (sub)question.
This paper consists of 5 printed pages

Question 1:
a) Define the following terms
i) A computer program
ii) A compiler
iii) An interpreter
.................................................................................................................................. [2+2+2]
b) Write a C++ program or a function that uses a FOR loop to find a product of integer
numbers between any two integers,
(
) with a specified incremental
step, k.
........................................................................................................................................ [14]
Question 2:
a) Identify all syntax errors in the following program
#include <iostream >
int m ain()
{
cout << CS2431 Test 01';
}

.......................................................................................................................................... [9]
b) The computation

, is the arithmetic

, for the integers y

and n.
Write a C++ program or function that uses a WHILE loop to compute
.
........................................................................................................................................ [11]
Question 3:
a) Consider the following program segment
int sum = 0;
for(int k = 1; k <= n; k++)
{
cout << k = << k << sum = << sum << endl;
sum += k;
}

Write down the output of this loop for n = 4.


CS 2431 - Programming Fundamentals

28th Sep 2005

Test 01

Page -1/5-

.......................................................................................................................................... [8]
b) A factorial of a natural number n is defined as:

Using a DO ... WHILE loop write a program or a function computing n!


........................................................................................................................................ [12]
Question 4:
a) An insurance company issues policies depending on the age and gender of its clients. If
the client is female and below 40 years of age, the company issues Policy A (i.e. the
most attractive package), otherwise it issues Policy B.
Write a C++ IF statement that could be used for issuing these insurance company
policies. Assume that age (type int) and gender (type char) would be used as appropriate
variable names. Use the C++ cout statement to print out the appropriate message to issue
the corresponding policy.
........................................................................................................................................ [10]
b) Write down the output for the following nested FOR loop
for( int i = 1; i < = 3; i++)
for( int j = 1; j < = i; j++)
cout << i << << j << << i * j;

........................................................................................................................................ [10]
Question 5:
a) Say whether true (T) or false (F) in the following
i) A C++ variable can be an underscore character
ii) In C++, any arithmetic expression evaluating to a non-zero value is regarded as
representing the truth value: true.
iii) The two Boolean constants: true or false are integer expressions.
.................................................................................................................................. [2+2+2]
b) In a college, graduates are classified as Pass with Credit, Pass or Fail. The
classification is based upon the performance mean mark of the student. The
classification is as follows:
Mean Mark

Classification

65 or above

Pass with Credit

50 64

Pass

below 50

Fail

Using an IF statement construct, write a program or a function that accepts a mean mark
(type int) from the user and then outputs the appropriate classification message.
........................................................................................................................................ [14]

CS 2431 - Programming Fundamentals

28th Sep 2005

Test 01

Page -2/5-

Question 6:
Write a C++ program or function that can determine if the given number is prime or not as
follows:
1. The program accepts a number, N, from the user
2. The program then outputs a message whether N is prime or that N is not prime
HINT:
1.

, N is not prime iff


such that p is a co-factor of N,
meaning N mod p = 0.
2. Every non-prime number N can be written as a product of its prime factors only.
3. All primes are odd, except the least prime, 2.
........................................................................................................................................ [20]
Question 7:
At NUL students are assessed and classified based upon an individuals overall weighted
mean (OWM) which is computed as:

The classification is as follows:


OWM

Classification

75 or above

First Class

70 74

Second Class, First Division

60 69

Second Class, Second Division

50 59

Pass

0 49

Fail

Write a C++ program or function that can classify students at NUL as follows:
1. The program allows the user to enter each course mark (type int) and its corresponding
credit hours (type int) a pair at the time, until the user has finished all the courses for this
particular student.
2. The program then displays the classification of the student based on the calculated
OWM and the given classification rules.
3. The program then allows the user to enter course information for the next student or quit
the program.
........................................................................................................................................ [20]
CS 2431 - Programming Fundamentals

28th Sep 2005

Test 01

Page -3/5-

CS 2431 - Programming Fundamentals

28th Sep 2005

Test 01

Page -4/5-

CS 2431 - Programming Fundamentals

28th Sep 2005

Test 01

Page -5/5-

Potrebbero piacerti anche