You are on page 1of 20

The University of the South Pacific

Serving the Cook Islands, Fiji, Kiribati, Marshall Islands, Nauru, Niue, Samoa, Solomon Islands, Tokelau, Tonga, Tuvalu, and Vanuatu.

School of Computing, Information and


Mathematical Sciences

CS111: Introduction to Computing Science

FINAL EXAMINATION – SEMESTER 1, 2013

Time Allowed 3 hours plus 10 minutes reading

100 marks (60% of final grade)


INSTRUCTIONS

1. This exam has three sections:


a. Section A: 40 marks
b. Section B: 30 marks
c. Section C: 30 marks
2. Answer all questions in sections A, B and C. There are no choices.
3. Write your answers spaces provided in this booklet. You can ask for
more paper if required.
4. This exam paper has a cover page, one page with instructions and
answer sheet for section A + 19 pages of questions.
5. The number of questions (Section A+B+C) is 21.
6. You can use calculators.
7. This exam is worth 60% of your overall mark. The minimum exam
mark is 40/100.

1
Name: Student Number: Page 1

Answer Key for Exam A


Section A (4 points each)

1. Who created the C++ programming language?


(A) Brian Kernighan
(B) Dennis Ritchie
(C) Bjarne Stroustrup
(D) Alan Turing
2. Consider the following code snippets:
(A) (B)

d = s - n^2; d = s - n * n;
p = d / 2 * n; p = d / 2 * n;
a = n + p; a = n + p;
n_new = a - p^2/(2 * a); n_new = a - p * p/2 * a;

(C) (D)

d = s - pow(n,2); d = s - n * n;
p = d/2n; p = d / (2 * n);
a = n + p; a = n + p;
n_new = a - pow(p,2)/2a; n_new = a - (p * p)/(2 * a);

Which of these code snippets is syntactically correct and implements the following computation:

d =s − n2
d
p=
2n
a =n + p
p2
nnew =a −
2a

(A) Snippet (A)


(B) Snippet (B)
(C) Snippet (C)
(D) Snippet (D)
Name: Student Number: Page 2

3. (A)

cout << "It is winter now\nYou are sitting an exam\nJust three more hours\n";

(B) (C)

cout << "It is winter now\n"; cout << "It is winter now" << endl;
cout << "You are sitting an exam\n"; cout << "You are sitting an exam" << endl;
cout << "Just three more hours\n"; cout << "Just three more hours" << endl;

Which of these code snippets has as output:


It is winter now
You are sitting an exam
Just three more hours
(A) Snippet (A)
(B) Snippet (B)
(C) Snippet (C)
(D) All of the above
4. Why should you define test cases?
(A) To test whether the program runs.
(B) To test whether to the program compiles.
(C) To test whether the program contains a syntax error.
(D) To test whether the program produces the expected outcome.
5. Consider the following snippet:

int i = 0;
int rank[3]={2,1,0};
while(i < 3 && rank[i]>0){
cout << rank[i] << ",";
i++;
}

What is its output?


(A) 2,1,0,
(B) 2,1,
(C) 2,
(D) It will print nothing.
Name: Student Number: Page 3

6. (A) (B)

int max(int num1, int num2){ int max(int num1, int num2){
if(num1 > num2){ if(num1 > num2){
max = num1; return max;
} }
else { else {
max = num2; return min;
} }
} }

(C) (D)

int max(int num1, int num2){ int max(int num1, int num2){
if(num1 > num2){ if(num1 > num2){
max = num1; return num1;
} }
else{ else {
max = num2; return num2;
} }
return max; }
}

Which of these functions is syntactically correct and returns the maximum on num1 and num2?
(A) Function (A)
(B) Function (B)
(C) Function (C)
(D) Function (D)
Name: Student Number: Page 4

7. Consider the following snippets:


(A) (B)

int amount = 75000; int amount = 75000;


if(amount >= 50000){ if(amount >= 50000){
cout << "30% marginal tax\n"; cout << "30% marginal tax\n";
} else if(amount >= 10000){ }
cout << "10% marginal tax\n"; if(amount >= 10000){
} cout << "10% marginal tax\n";
}

(C) (D)

int amount = 75000; int amount = 75000;


if(amount >= 50000){ if(amount >= 10000){
cout << "30% marginal tax\n"; cout << "10% marginal tax\n";
if(amount >= 10000){ if(amount >= 50000){
cout << "10% marginal tax\n"; cout << "30% marginal tax\n";
} }
} }

Which of these has as output the single line


30% marginal tax

(A) Snippet (A)


(B) Snippet (B)
(C) Snippet (C)
(D) Snippet (D)
8. At what stage of software development do you write pseudo code?
(A) Compiling the program.
(B) Developing an algorithm.
(C) Maintenance.
(D) Testing the program.
Name: Student Number: Page 5

9. Consider the following programs:


(A) (B)

void lift(int array[]){ void lift(int array){


array[0]++; array[0]++;
} }
int main(){ int main(){
int marks[3]={1,1,1}; int marks[3]={1,1,1};
lift(marks); lift(marks);
return 0; return 0;
} }

(C) (D)

void lift(int array[3]){ void lift(int array[]){


array[0]++; array[0]++;
} }
int main(){ int main(){
int marks[3]={1,1,1}; int array[]={1,1,1};
lift(marks[0]); lift(int array[]);
return 0; return 0;
} }

Which of these handles arrays syntactically correct, i.e. which of these will compile?
(A) Program (A)
(B) Program (B)
(C) Program (C)
(D) Program (D)
Name: Student Number: Page 6

10. (input.txt)

Mark
Green
Seven

(in2output.cpp)

# include <iostream>
# include <fstream>
using namespace std;

int main(){
ifstream infile;
infile.open("input.txt");

ofstream outfile;
outfile.open("output.txt");

cout << "Processing input!\n";


outfile << "The input file contains the words: ";

string word;
while(infile >> word){
outfile << word << ", ";
}
cout << "Done!\n\n";
infile.close();
outfile.close();
system("pause");
return 0;
}

The program in2output.cpp reads the file input.txt and writes to file output.txt. What will be the
content of the file output.txt after the program runs?
(A) The input file contains the words: Mark, Green, Seven,
(B) Processing input!
Done!
(C) Processing input!
The input file contains the words: Mark, Green, Seven,
Done!
(D) Processing input!
The input file contains the words: Mark, Green, Seven,
Done!
Press any key to continue . . .
Name: Student Number: Page 7

Section B (5 points each)

11. Suppose that the tax rate is computed according to the following table:

Income level Tax level


Income less than $10000 Tax is $0
Income between $10000 and $50000 Tax is 10% of the income in excess of $10000
Income above $50000 Tax is $4000, plus 30% of the income in excess of $50000

Figure 3: Table with income and tax levels

Assume that a colleague has written the following program to compute the tax:

0 int main(){
1 double income, tax = 0;
2 const double RATE1 = 0.1;//10%
3 const double RATE2 = 0.3;//30%
4
5 cout << "Tax calculator\n";
6 cout << "What is you gross income?";
7 cin >> income;
8
9 if(income >= 10000){
10 tax = (income - 10000) * RATE1;
11 }
12 else if (income >=50000){
13 tax = 4000 + (income - 50000) * RATE2;
14 }
15 cout << "Your tax is: " << tax << endl;
16 system("pause");
17 return 0;
18 }

Figure 4: Tax calculation program

Users are complaining that it does not compute the correct tax for amounts above $50000. You are
asked to see if you can find the error.
Please answer the following questions: (1) Give an example income, for which the wrong tax will be
computed. What is the value you expect, what is the value the program computes? (2) What is the
problem with this program; what is the mistake the program makes? Refer to the line numbers. (3)
Which lines do you need to change to correct the error? Describe the solution briefly.
Name: Student Number: Page 8

Answer question 11 here

(1) For an income of $100000 the tax should be $19000. However, the program computes it
as $9000. [1 point] (2) The problem is that the programs first checks if the income is above
10000, and only later if it is above 50000. Since any income above 50000 is also above 10000,
the condition in line 9 will be true, and hence the rate of 10% applied. [2 points] (3) To fix this
problem the program should first check if the income is above 50000, and otherwise check if it is
above 10000. The if in lines 12 to 14 should come first, and the if from line 9 to 11, should be
part of the else. [2 points]
Name: Student Number: Page 9

12. Consider the following code snippet:

double number;
cout << "Please enter a number: ";
cin >> number;

do{
number = -number/2;
cout << number << ",";
}while( /*condition*/ );

Give a boolean expression to replace /*condition*/, such the program stops if number is greater than
-0.001 and smaller than 0.001.

(number<=0.001 || initial=>-0.001) or !(number>-0.001 && number<0.001)

13. Consider the following code snippet:

int main()
{
1 double square = 9;
2 double root = 3;
3 double error = 1;
4 while(error > 0.1 || error < -0.1){
5 root = (root + square/root)/2;
6 error = root*root - square;
}
7 cout << "The root is: " << root;
8 return 0;
}

The numbers on the left are line numbers.


Please provide the next 5 lines of the hand trace in the space below:
line square root error comment
1 9 initialising square to 9
2 ” 3 initialising root to 3
3 ” ” 1 initialising error to 1
4 ” ” ” Condition is true
5 ” 3 ” (3 + 9/3)/2 is 3
6 ” ” 0 3 ∗ 3 − 9 is 0
4 ” ” ” Condition is false
7 ” ” ” Print The root is: 3
8 ” ” ” The end
Name: Student Number: Page 10

14. Consider the following program:

int main()
{
1 int index;
2 double cost;
3 int days=0;
4 double rate[3]={79.90,99.90,149.90};

5 do{
6 cout << "Which rate do you want (1-3)";
7 cin >> index;
8 } while( index < 1 || index > 3);

9 if(index==1){
10 days = 7;
11 cost = (cost * rate[index])/days;
12 }
13 else if(index==2){
14 cost = rate[index]/days;
15 }
16 else{
17 cost = rate[index];
18 }
19 cout << "The cost is: " << cost << endl;
20 return 0;
}

The numbers on the left are line numbers.


This program compiles and runs, but this program has still three common errors:
(a) Possible array bounds violation.
(b) Possible division by zero.
(c) Possible use of an uninitialised variable.
Each of these errors occurs exactly once. Describe where in the program each of these errors occurs,
which variables are involved, and what the effect of the errors on the output for the cost is.
Name: Student Number: Page 11

Answer question 14 here

(a) The first error occurs in line 17. Variable index can have value 3, which means that
rate[index] would access an element outside of the bounds of the array. [1 point] (b) A possible
division by zero occurs in line 14. Variable days was initialized to zero, and is used here. [1
point] (c) This error occurs in line 11. Variable cost on the right hand side has no value yet. [1
point] The effect will be that the cost is likely to contain a nonsense result. [1 point] This is true
for all of the three errors [1 point].
Name: Student Number: Page 12

15. Consider the following program:

int main()
{
1 double speed, fx=0;
2 const double MAXSPEED1=60;
3 const double MAXSPEED2=70;
4 const double RATE1 = 5;
5 const double RATE2 = 12;

6 do{
7 cout << "Please enter a speed: ";
8 cin >> speed;
9 } while(speed <0);

10 if(speed>=MAXSPEED1){
11 if(speed <= MAXSPEED2){
12 fx = (speed - 60)* RATE1;
13 cout << "Your fine is : " << fx << endl;
14 }
15 else{
16 fx = (speed - 70)* RATE2;
17 cout << "Your fine is : " << fx << endl;
18 }
19 } else
20 cout << "You didn’t drive too fast.\n";
21 cout << "Have a nice day\n";

22 system("PAUSE");
23 return 0;
}

The numbers on the left are line numbers.


This program compiles and the executable runs and computes the fine correctly. However, there are
five problems with programming style.
(a) Use of magic constants.
(b) Unnecessary duplication of code.
(c) Cryptic variable name.
(d) Poor indentation.
(e) Poor use of brackets.
Describe exactly where each of these problems occurs, and explain the issue.
Name: Student Number: Page 13

Answer question 15 here

(a) Magic constants are used in lines 12 and 16. Constants MAXSPEED1 and MAXSPEED2
should have been used. (b) The lines 13 and 17 are unnecessary duplicates. They could be moved
to the end of the if, between line 18 and 19. (c) Variable fx is a cryptic name. It should be
changed to a meaningful name like fine. (d) The entire if from line 11 to 19 is poorly indented.
(e) The else branch in line 20 should use brackets. It seems like line 21 is intended to be part of
the else, but that is not the case. [1 point each]
Name: Student Number: Page 14

16. This is a report on a software development project.

Last year a security hole in the housing administration database led to more
than 100 confirmed cases of identity theft. We were asked to investigate the role
of ACME Inc, which provided the software for the tenant database. This report
summarizes our findings in the light of the ACS Code of Conduct .

The unauthorized access to the database was made possible by using an outdated
authentication method for the database. Management of ACME Inc had been
informed of this problem at least 3 years ago. The fact that they did not share
this information with the housing authority was not only dishonest , it
also violated the primacy of the interest of the public .

Interviews with developers at ACME Inc revealed that none of them has had a
security training in the last 10 year. It is apparent that ACME Inc does not
invest in professional development of their employees. The fact that
they have not been able to fix the problem since it was reported in the news three
months ago also raises doubts about their competence .

We advise that ACME Inc should be held liable for negligence, and should
be excluded from any future government contract until they can demonstrate
commitment to professional standards.
Please fill in the blanks. Use 5 words or phrases from the following list:
Coding guideline, code of conduct, textbook, board of directors, syntax error, logical error,
efficient, dishonest, affordable, excellent, intoxicated, private, public, shareholders, family,
offshore mining, professional development, object oriented programming, foreign currencies,
fish, competence, cheerfulness, affordability, funding, income, fine, Dennis Ritchie, Brian
Kernighan, Alan Turing, Bjarne Stroustrup.
Name: Student Number: Page 15

Section C (6 points each)


This section asks you to develop a program that implements the Bakhshali approximation for computing a
square root. This algorithm was found in an ancient Indian manuscript from before 3rd century AD. The
core idea of the algorithm is that if you are given a number - the square - and an initial guess for the root,
you can improve on the guess.
Let s be the square, and g be the current guess. A better guess gnew is computed as follows:

d =s − g 2 (1)
d
p= (2)
2g
a =g + p (3)
2
p
gnew =a − (4)
2a
The algorithm starts with an initial guess, and repeats the routine until the error s − g 2 gets close to
zero. For example, suppose you want to compute the square root of 10 with an initial guess of 3. The error
would be 10 − 32 = 1. You can then use the formulas (1) to (4) to improve the guess to 3.16228070175439,
which has an error of less than 0.0001. If we use this guess for the next iteration we get as next guess
3.16227766016838. It has an error smaller than 10−14 , which is about as small as the numerical accuracy of
most computers.
Your task is to write two functions and use them in the main function to compute the Bakhshali approx-
imation. The functions should be the following:
• double next guess(double s, double g); This function returns for a number s and a guess g an
improved guess gnew using the formulas (1) to (4).

• bool almost zero(double s, double g); This function returns true if s-g*g is smaller than 1e-10
and bigger than -1e-10, and it returns false otherwise.
The main function should adhere to the following specification:

It should ask the user for a number and an initial guess. It should then compute and display a
new guess until the error is almost zero. Finally, the program should display the last guess, and
the error.
The output of the program should look like this:

Please enter a number: 10


Please enter a guess for the root: 3

Guesses for the root of 10


3
3.16228070175439
3.16227766016838

The final approximation is : 3.16227766016838


The error is : 1.60201713006458e-015
Name: Student Number: Page 16

17. Which variables do you need for the main function of this program, and what are their types?
You need at least the following two variables: double square, double root. A variable double
error could be useful, or a boolean to indicate if you can stop. For the variable of type double
you can use other floating type such as float. Of course, the variable names can be chosen
differently. Marking: 2 points for identifying the main variables, and 2 points for the correct
type. 2 points if no unnecessary constant or variables are defined.

18. Write a design for the main function in pseudo-code. Your solution should consider (1) the specifi-
cation of the main function on the previous page, and consider (2) which steps have to be repeatedly
executed until the program can stop, and consider (3) the fact that some steps should be performed
by functions.

Ask for number


Ask for guess
While error is not almost zero
Compute new guess
Display guess
Display final guess
Display error

Marking: 1 points if the pseudo code is not C++. 2 points if it uses either a do or while loop. 2
points if it is correct. 1 point if it has the right level of detail.
Name: Student Number: Page 17

19. Write C++ code for two functions. First, write function double next guess(double s, double g);
which computes a new guess given number s and guess g. Please provide the function in the space below:

double next_guess(double s, double g){


double d = s - g * g;
double p = d/(2*g);
double a = g + p;
double newvalue = a - (p*p)/(2*a);
return newvalue;

Marking: 1 point if the parameter definition and use of return is correct. 1 point if formulas are
correct. One point if there are no other serious syntax errors.

Second, write function bool almost zero(double s, double g); which returns true if the error is
smaller than or equal to 1e-10 and larger than or equal -1e-10, and false otherwise. Please provide
the function in the space below:

bool almost_zero(double s, double g){


double error = g*g - s;
const double EPS = 1e-10;
if(error <= EPS && error >= -EPS){
return true;
}
else{
return false;
}
}

Marking: 1 point if the parameter definition and use of return is correct. 1 point if formulas are
correct. One point if there are no other serious syntax errors.
Name: Student Number: Page 18

20. Write a the main function int main(). Take into consideration your pseudo-code, and use, if possible,
the functions next guess and almost zero.

int main()
{
double guess;
double square;
cout << "Please enter a number: ";
cin >> square;
cout << "Please enter a guess for the root: ";
cin >> guess;

cout << "\nGuesses for the root of " << square << endl;
cout << setprecision(15) << left << setw(15) << guess << endl;
while( !almost_zero(square,guess)){
guess = next_guess(square,guess);
cout << left << setw(15) << guess << endl;
}

cout << "\nThe final approximation is : " << guess << endl;
cout << "\nThe error is : " << setw(5) << square - guess * guess << endl;
system("PAUSE");
return 0;
}

Note: The formatting directives are optional.


Marking: 2 points if the functions are used, and used correctly correct. 2 point if the function
has the correct structure. Two points if there are no other serious syntax errors.
Name: Student Number: Page 19

21. Present three pairs of a number and an initial guess that you can use to test your program. For each
pair describe why you select that pair as an input for a test case. Note: In this exam there is no
need to predict the outcome. All that is asked for is an example input, and why you select it.
Please provide the first test input with an explanation:
The first test is to use as number 10 and as guess 3. This is an example of a normal expected
input. It should find a good iteration quickly.

Please provide the second test input with an explanation:


The second test is to use as square the number 1 and as guess the number 0. It is a good practice
to see if the algorithm can deal with unusual inputs, such as zero.

Please provide the third test input with an explanation:


The second test is to use as square the number -9 and as guess the number 1. It is a good practice
to see if the algorithm can deal with unusual inputs. The root of negative numbers is not defined,
and it would be good to test what the program does in such cases.

You might also like