CIS
1.5
Test 2 Sample
page 1 of
7
Name ___________________________

1. Show what is printed by the following program. If a storage location has not been assigned a value, use ?? when asked to print its value. (12 pts)

#include <iostream>

int main()

{

int arr[10];

for (int j = 2; j <= 8; j++)

arr[j] = 10 * j;

for (int k = 0; k <= 9; k++)

cout << arr[k] << " ";

cout << endl << endl;

arr[7] += 200;

arr[4]--;

arr[0] = 7;

arr[1] = 5;

arr[9] = 21;

arr[3] = arr[2] * 2;

for (int k = 0; k <= 9; k++)

cout << arr[k] << " ";

cout << endl << endl;

for (int j = 0; j <= 8; j += 2)

if (arr[j] < arr[j+1])

arr[j]++;

else

arr[j] = -2;

for (int k = 0; k <= 9; k++)

cout << arr[k] << " ";

cout << endl << endl;

return 0;

}

2. Show what is printed by the program shown below. (11 pts)

#include <iostream>

void change(int &, int &, int);

int main()

{

int a=10, b=12, c=6;

change(a,b,c);

cout<< a<<" "<<b<<" "<< c<<endl;

a = 5; b = 17; c = 1;

change(c,b,a);

cout<< a<<" "<<b<<" "<< c<<endl;

return 0;

}

void change(int &a, int &b, int c)

{

a += 6;

c--;

if (a > b)

a = 22;

else if (b < c)

a = 15;

else

b = 100;

c += 2000;

cout << "in change " << a <<

" "<< b <<" "<< c << endl;

return;

}

3. Show exactly what is printed by the following program. (11 pts)

#include <iostream>

#include <string>

int main()

{

string one = "GHIJKL";

string two = "ABCDE";

string three = "catfood";

string four;

int m,k;

two = two + one;

cout << one << " "<< two <<" "

<< three << endl;

if (one > two)

cout << "case 1" << endl;

else

cout << "case 2" << endl;

k = one.length()+

4* two.length();

cout << k << endl;

m = three.find("food",0);

cout << m << endl;

three.insert(2,"nned");

cout << three<< endl;

one.erase(3,2);

cout << one << endl;

four = two.substr(2,4);

cout << four << endl;

two = "baseball";

two[3]++;

two[4] = 'Q';

two[5] = '9';

cout << two << endl;

m = two.length();

for (int i = 0; i < m; i++)

cout << two[i] << endl;

cout << endl;

return 0;

}

4. a. Show what is printed by the following program. For partial credit, you must show how you evaluated each of the conditions tested. (9 pts)

#include <iostream>

int main()

{

int x=3, y=30, z=17;

if (z - x > y && x != 2)

cout << "einstein"<< endl;

else

if (x + z <= y - 12 || y == 18)

cout << "heinlein" << endl;

else

cout << "stephenson" << endl;

cout << "cyberspace" << endl;

return 0;

}

b. Repeat part a assuming that the variables have these initial values:

x = 2; y = 18; z = 5;

c. Repeat part a for these values:

x = 1; y = 15; z = 20;

5. (12 pts) a. Write a function called easy(). The function will receive one parameter called st which is a string. It will replace every occurrence of the letter 'a' in the string st by 'X'. In addition, it will return the number of changes that were made.

For example, if the string matched to st is the following: "the cat in the hat hates rats", then the new string will be "the cXt in the hXt hXtes rXts" and the function will return 5.

b. Write a main program which will use the function. The main program will read in a string called str1. It will send the string to the function easy(). It will print the new string after return from the function, and it will print the value returned by the function. Supply one comment in the main program and one in the function.

6. Number systems (10 pts)

a. 101011 in base 2 is equivalent to _______________ in base 10. b. 94 in base 10 is equivalent to ____________ in base 2.

c. 12C in base 16 is equivalent to ____________ in base 10.

d. Assume that you have a number which is represented using 4 hexadecimal symbols (e.g., F7A9 or 4C6E). What is the largest number of bits that will be required to represent this hexadecimal number?

e. Perform the following addition of two binary (base 2) numbers. Your answer should be a number in binary.

1110 + 1101 = ?

7. Write a complete C++ program (including comments in the main program and in the functions -- 3 points) to do the following (35 pts):

1. Write a function called readdata() which will work with two variables, an integer n and a one-dimensional array vals, which contains up to 50 real numbers. The function will read a value for n, then read n real numbers. It will store the data values in the array vals. The function will print the original data values as they are read in.

2. Write a function called countem() which will receive three parameters: an integer n, a value specval, and an array vals. The function will count how many of the first n elements of the vals array are larger than specval. Print each value that is larger than specval. Also, print (either in the main program or in the function) the number of values which are larger than specval.

For example, if the array holds 66 10 -7 0 4 31, with n = 6, and specval = 4, then 66 10 and 31 are larger than specval(). There are 3 values in the array which are larger.

3. Write a function called multiplyall() which will multiply each element stored in the array by 5. (The function receives the usual parameters--an array and an integer giving the size of the array.)

For example, assume the array holds: 66 10 -7 0 4 31, with n = 6; after the function call, the array holds 330 50 -35 0 20 155.

4. Write a function called findavg() which will find the average of the elements in an array (the usual parameters). The function will return the answer to the calling program.

5. Write a function sortem() which receives two parameters, an array and n, the number of filled positions in the array. Sort the array in ascending numeric order.

Write a main program which will call these functions.

1. First the main program will call readdata() (see part 1) to read a set of data containing num elements into an array which the main program calls numbers.

2. Then it will call the function countem() (see part 2) to find how many of the first num array elements are greater than 20.

3. Then the main program will call multiplyall() (see part 3) to modify the numbers array. The new values in the array should be printed (either in the main program or in the function).

4. Then the main program will call findavg() (see part 4) to find the average of the first num values in the numbers array. The main program should print the average.

5. The main program will call the function countem() again, to determine how many items in the changed array are more than 50.

6. The main program will call the function sortem() to sort the first n valuess in the numbers array. Upon return to main, print the sorted array.

The main program will simply call the functions, making sure that all parameters have the appropriate type, etc. Assume that the array has no more than 100 real numbers. Make sure that each function receives the right parameters, with the right types, etc.