CS110 EXAM 2 Solutions

 

Written below are all the answers to the questions (in blue color). If you have any questions and/or clarifications, please come and talk to me.

Question 1. (10 points)

A.The parameters used to define a function are called formal parameters.

B. The parameters used in a procedure or function invocation are called actual parameters.

C. In the following program segment, what set of values of X will cause to be assigned the value 50? (X=13, X != 13, X < 13, all values of X, no values of X)

if (X == 13)
	Y = 4;
Y = 50 ;

ALL VALUES OF X.

D. Assuming that A=10, B=15, C=12, and D=17, what is the value of the condition below? (True/False)

(((A - B) != 0) && ((C > 10) || (C+D == 20)))

TRUE.

E. A variable that is known only within the function in which it is defined is called a local variable.

F. What is the keyword used in a function header to indicate that a function does not return a value or to indicate that a function has no parameters: void.

G. What is the name of the function used to produce random numbers? rand

H. What is the name of the function used to seed the random number generator? srand

I. The following statement can be used to increment the value of an int variable X by 1.

X = X + 1;

Write two other ways of doing the same thing in C++.

X++;
++X;
X += 1;

Question 2. (10 points)

A. Write a C++ condition (only) which is true only when an int variable A is between 17 and 57, inclusive.

((A >= 17) && (A <= 57)) 

B. Write a syntactically correct prototype of a float-valued function called Sum that has two value parameters of type float.

float Sum(float x, float y);

Question 3. (10 points)

Rewrite the following if-statement as an equivalent switch-statement. Assume Digit is an int and value is a char.

if (Digit == 6)
	value = 'A';
else if ((Digit >= 0) && (Digit <= 2))
	value = 'C';
else if ((Digit == 4) || (Digit == 5))
	value = 'B';
else
	value = 'D';

switch (Digit) {
	case 6: value = 'A'; break;
	case 0:
	case 1:
	case 2: value = 'C'; break;
	case 4:
	case 5: value = 'B'; break;
	default: value = 'D';
}

Question 4. (10 points)

The following C++ program segment is supposed to print the first N integers, but it doesn't work. Correct the statements so that they achieve the intended effect.

M = 1;
while (M <= N) {
	cout << M << endl;
	M = M + 1;
}

Question 5. (10 points)

Rerwrite the C++ statements below using a while-loop to do the same task.

Product = 1;
M = 6;
for (int Next = 1; Next <= M; Next++)
	Product = Product * Next;

Product = 1;
M = 6;
Next = 1;
while (Next <= M) {
	Product *= Next;
	Next++;
}

Question 6. (10 points)

What is the output of these C++ statements (write in the box below):

for (int k = 5; k >= 1; k--) {
	for (int i = 1; i <= 5 - k; i++)
		cout << ".";
	for (int j = 1; j <= 2 * k - 1; j++)
		cout << "B";
	cout << endl;
}

BBBBBBBBB
.BBBBBBB
..BBBBB
...BBB
....B

Question 7. (10 points)

Write a C++ function called WriteDigit that takes an int parameter whose value is between 20 and 29 (inclusive) and prints out the corresponding number in words. For example:

WriteDigit(23);

will print out the word:

twenty-three

void WriteDigit(int n) {
	cout << "twenty";
	switch (n) {
		case 21: cout << "-one"; break;
		case 22: cout << "-two"; break;
		case 23: cout << "-three"; break;
		case 24: cout << "four"; break;
		case 25: cout << "-five"; break;
		case 26: cout << "-six"; break;
		case 27: cout << "-seven"; break;
		case 28: cout << "-eight"; break;
		case 29: cout << "-nine"; break;
	}
	cout << endl;
}

Question 8. (10 points)

What is the output from this C++ program?

int CleverSwap(int& X, int& Y); 

main() {
	int A, B;

	A = 25;
	B = 15;
	cout << "A = " << A << " and B = " << B << endl;

	CleverSwap(A, B);
	cout << "A = " << A << " and B = " << B << endl;

	A = B;
	CleverSwap(A, B);
	cout << "A = " << A << " and B = " << B << endl;

	CleverSwap(A, A);
	cout << "A = " << A << " and B = " << B << endl;
}

void CleverSwap(int& X, int& Y) {
X = X + Y;
Y = X - Y;
X = X - Y;
}

A = 25 and B = 15
A = 15 and B = 25
A = 25 and B = 25
A = 0 and B = 25