You are on page 1of 6

The American University in Cairo

Computer Science & Engineering Department


CSCE 106 04/10/12

Dr. Rania Mameesh Midterm-II Fall 2009

Last Name: ........................................................... ID: ...................................

First Name: .......................................................... Section: ...........................

EXAMINATION INSTRUCTIONS
* Do not turn this page until asked to do so.
* Exam time is 60 minutes.
* Put the answers on the same question sheet; do not use any additional papers, even for scratch.
* Please make sure that you wrote your name, section, and ID in the indicated places.
* No electronic devices, cell phones, calculators, etc.
* Read the honesty policy.
* Sign the following statement.

HONESTY POLICY
Cheating in Exams is a violation of the honesty policy of AUC. Whispering, talking, looking at someone else‟s
paper, copying from any source, or prior knowledge of the contents of the exams is considered cheating.
Anyone who does any of these actions or her/his answers indicates that she/he did any of them, will receive a
punishment ranging from zero in this exam to failing the course. If repeated, it may lead to dismissal from
AUC.

I have read the honesty policy and exam instructions and I am presenting this exam as entirely my effort.

Signature : _______________
---------------------------------------------------------------------------------------------------------------------
DO NOT USE THIS SECTION
Group Points Grade
1 5
2 5
3 5
4 5
5 15
6 15
7 25
8 25
Total 100

1
1. (5 points) Write a complete C++ program to draw two circles separated by a line, as shown in the figure. The
program has to have a defined function to draw a circle on the screen.

*
#include <iostream> void drawCircle(){ * *
using namespace std; cout<<" * "<<endl; **
------
void drawCircle(); cout<<"* *"<<endl; *
* *
void main(){ cout<<" * * "<<endl; **
drawCircle(); }
cout<<"--------"<<endl;
drawCircle();
}

2. (5 points) Write a C++ program fragment that uses nested loops to produce the following output:

int col, row; 0 0 0 0 0 0


for (row=0; row<6; row=row+1){ 1 1 1 1 1 1
for (col=0; col<6; col=col+1) 1 1 2 2 2 2
if (col >= row) 1 1 1 3 3 3
cout << setw(2) << row; 1 1 1 1 4 4
else 1 1 1 1 1 5
cout << setw(2) << 1;
cout<<endl;
}

3. (5 points) Rewrite each of the following boolean expressions eliminating the not operator.
a. (! (s >= t)) = (s<t)
b. (! (! (x <= y ) || (x > 10))) = (x<=y)&&(x<=10)

4. (5 points) What is the value of the following expressions? Please show your draft work for evaluation.
a. ((3 + 5) < (21 - 9) && false || (! (true && false))) = (8< 12 && false || (! (false))) =true && false || true
=false || true =true
b. (x – 5 != 5) && (x – 5 == 5) = One of these 2 expressions will be true and the other will be false depending
on the value of x. So it will either be (true && false) or (false && true). In both cases the answer will be false.

5. (15 points) Please list the following:


a. Function prototype is composed of: return type, name, and input arguments.
b. Function definition is composed of: header and body.
c. A function is only executed when it is called.
d. When a function call executes, the value of each actual parameter is passed into the corresponding
formal parameter of the function definition.
e. Functions that compute something needed by the caller must have a return statement.
f. Local variables are visible only in their functions, global variables are visible everywhere.
g. When evaluating logical expressions, C++ uses a technique called short-circuit-evaluation. This means
that evaluation of a logical expression stops as soon as its value can be determined.
h. An infinite loop executes endlessly. It can be avoided by making sure that the loop‟s body contains
statement(s) that assure that the exit condition will eventually be false.
i. m = 3; n = ++m; k = m++; //value of n is 4 and value of k is 4.

6. (15 points) Write a C++ program that takes as input two numbers and an operation to be performed on those
two numbers (+, -, *, and / for addition, subtraction, multiplication, and division, respectively). It outputs the
numbers, the operator, and the result. Format your output to two decimal places. Implement a function divZero
that detects division by zero. If division by zero occurs, main function must display “division by zero” message
then exits the program. Implement another function calculator that calculates the result of addition, subtraction,

2
multiplication, and division using switch case. The main function must output the result of the function
calculator on the screen.

#include<iostream>
using namespace std;

float calculator(float op1, float op2, char operator);


int divZero(float op2, char operator);

void main() {
float o1, o2;
char o;
float c_result;
cout << “Please enter operand 1: \n”;
cin >> o1;
cout << “Please enter operand 2: \n”;
cin >> o2;
cout << “Please enter operation +, -, *, /: \n”;
cin >> o;

if (divZero(o2, o) == 0) {
c_result = calculator(o1, o2, o);
cout << o1 << o << o2 << „=‟ << c_result;
}
else
cout << “Division by zero \n”;
}

float calculator(float op1, float op2, char operator){


float result;
switch(operator){
case „+‟: result = op1 + op2; break;
case „-„: result = op1 – op2; break;
case‟*‟: result = op1 * op2; break;
default: result = op1/op2;
}
return (result);
}

int divZero(float op2, char operator){


if (op2 == 0 && operator == „/‟)
return(1);
else
return(0);
}

3
7. (25 points) Draw a flow chart and write a program that converts a decimal number (integer) to its equivalent
binary number. The program should prompt the user to enter a decimal number greater than 0, and should ask
user to re-enter a valid number every time he/she enters an invalid number. When, user enters a valid number it
then computes the binary equivalent and outputs it digit by digit on the screen in a separate line.

#include <iostream>
using namespace std;
int lessZero(int number);
void main() { start
int decimal;

do { input
cout << “Please enter a decimal number greater than zero: \n”; decimal
cin >> decimal;
}
while (lessZero(decimal) == 1);
while(decimal != 0) { no yes
decimal
cout << (decimal % 2) << “\n”; <= 0
decimal = decimal/2;
}
}
int lessZero(int number){
no yes
if (number <= 0) decimal
return(1); != 0
else
return(0); binary =
} end decimal % 2

output
binary

decimal =
decimal / 2

4
8. (25 points) Show output of the following C++ program snippets. Show all tracing on exam sheets.

int j, k; Tracing table


int x = 2; j k x Other
for ( j = 0; j < 3; j = j + 1 ){ 2
switch (j){ 0 j<3 true;
case 0: 5 k>=x true
for (k = 5; k >= x; k = k – 1){ 4 k>=x true
cout << k; 3 k>=x true
cout << endl; 2 k>=x true
}
1 k>=x false
break;
1
case 1:
2 j<3 true;
while ( x < 9){
k = 3; x<5 true
while (k < x){ 8 k< x false
k = k + 1; 3 x<5 true;
cout << k; k< x false
} 4 x<5 true;
x = x + 1; k< x false
} 5 x<5 false
break; 3
case 2: 4 j<3 false
while ( x < 5){
k = 8;
while (k < x){ Screen Output
k = k + 1; 5
cout << k; 4
} 3
x = x + 1; 2
}
break;
}
j = j + 1;
}
const int ten = 10; Tracing table
int d, n = 7856; ten d n Other Other
while (n != 0){ 10 7856 n!=0 true
d = n % 10; 6 785 n!=0 true d == 5 false
n = n / ten; 5 78 n!=0 true d == 5 true
if (d == 5) 8 7 n!=0 true d == 5 false
continue; 7 0 n!=0 false d == 5 false
cout << d;
}

Screen Output
687

5
int x = 1, y = 1; Tracing table
while (x<3){ x y Other
y = y * x; 1 1 x<3 true;
cout << "x = " << setw(3) << x 1*1 = 1
<< "y =" << setw(3) << y 2 x<3 true;
<< endl; 1*2 =2
x = x +1; 2
} Screen Output 3 x<3 false
x = 1y = 1
x = 2y = 2

int a, b; Tracing table


float x; a b x Other
a = 5; 5 2 5/2=2
b = 2; 2 2%2 = 0;
a = a / b; 0 != 0 false;
if (a % b != 0) 3.75 - 2
x = 3.75 + a; 1.75
else
x = 3.75 – b;
cout << setw(4) << a << setw(4) << b Screen Output
<< setw(6) << x << endl; 2 2 1.75

#include <iomanip>
#include <iostream>
usingnamespacestd;
int main () {
double a; Screen Output
a = 2006.000000001; 2006.00
cout.precision(6); 2006.000000
cout<< a << endl; 2.006000e+003
cout<< fixed << a << endl;
cout<< scientific << a << endl;
}

You might also like