The AmericanUniversity in Cairo

Computer Science Department

CSCI 106-05

Dr. Eman Nasr / EXAM-III / Spring 2007
Last Name : ...... / ID: ......
First Name: ......

EXAMINATION INSTRUCTIONS

*Do not turn this page until asked to do so.

* Exam time is 75 minutes.

* Put the answers on the same question sheet, do not use any additional papers, even for scratch.

*Write your name, and ID in the indicated places.

*Calculators are not allowed.

*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, or copying from any source is considered cheating. Any one who does any of these actions or her/his answers indicate that s/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

Question / Points / Grade
I / 35
II / 10
III / 10
IV / 35
Total / 90

Question I (35 points)

Draw a flowchart and create a complete C++ modular program that contains the following:

a. Function swap that takes two integer numbers and swap them (interchange their values).

b. Function readPair that reads two integer numbers each of which should be greater than 0 and if the second integer is less than the first, the function swap is called to interchange the values of the two integers.

c. Function multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return true if the second is a multiple of the first, false otherwise.

d. Function main that will test k pairs of integer numbers. For each pair, the function readPair is called first to enter values for the two integer numbers, then the function multiple is called to test the two numbers. The program should print out the values of the two integers together with a message notifying whether the second integer is multiple of the first, or not

Question II (10 points)

Rewrite each of the following boolean expressions eliminating the not operator:

1)(! (s>=t))

2)(! (( s <= t ) || ( xy )))

3) (! (! (x <= y ) || (x > 10)))

Question III (10 points)

Write a C++ program fragment that uses nested loopsto produce the following output:

0 1 1 1 1 1

1 0 1 1 1 1

1 1 0 1 1 1

1 1 1 0 1 1

1 1 1 1 0 1

1 1 1 1 1 0

Question IV (35 points)

Please show the output of the following C++ program segments:

int k = 0, n = 6;
int L[n] = {3, 5, 6, 4, 9, 10};
bool valu = true;
while ( (k < --n) & (valu) )
{
if (L[k] > L[k+1])
{
valu = false;
cout < setw(2) < k < setw(2) <L[k] < endl;
}
else
k++;
}
if (!valu)
cout < “Ok. Out of the loop” < endl;
for (int k = 7; k <= 16; k++)
switch (k % 10)
{
case 0: cout < “, “;
break;
case 1: cout < “OFTEN “;
break;
case 2:
case 8: cout < “IS “;
break;
case 3: cout < “INDEED “;
break;
case 4:
case 9: cout < “DONE “;
break;
case 5: cout < “WELL”;
break;
case 6: cout < “.”;
break;
case 7: cout < “WHAT “;
break;
default: cout < “Bad number. “;
}
cout < endl;
int main()
{
int d = 17, e = 21,f = 15, a = 16, b = 19;
test (d, e, f);
cout < "In the main program after the first call, ");
cout < "the variables are: " < setw(3) < e < setw(3) < d < setw(3) < f < endl;
cout < “------“ < endl;
test (a, b, f);
cout < "In the main program after the second call, ");
cout < "the variables are: " < setw(3) < a < setw(3) < b < setw(3) < f < endl;
return 0;
}
void test(int t, int& s, int& x)
{
s = 4;
s = s + 3;
x = (++s + ++t);
t = 5 * s;
cout < "Function test Output:" < setw(3) < s < setw(3) < t < setw(3) < x < endl;
}

1