Professional Documents
Culture Documents
Serving the Cook Islands, Fiji, Kiribati, Marshall Islands, Nauru, Niue, Samoa, Solomon Islands, Tokelau, Tonga, Tuvalu, and Vanuatu.
1
Name: Student Number: Page 1
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
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;
int i = 0;
int rank[3]={2,1,0};
while(i < 3 && rank[i]>0){
cout << rank[i] << ",";
i++;
}
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
(C) (D)
(C) (D)
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");
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
11. Suppose that the tax rate is computed according to the following table:
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 }
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
(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
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.
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;
}
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;
}
(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
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;
}
(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
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
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:
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.
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:
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:
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;
}
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.