Name ___________________

 

CPTR246 Spring '00 (100 total points) Exam 2

 

1.     Template Functions: (25 points)

 

Write a template function named largest that takes three parameters of the same type and returns the largest. An example of its usage would be:

 

int i = 500;

int j = 600;

int k = 200;

cout << largest(i, j, k) << endl; // would display 600

 

 


2.     Command Line Arguments: (25 points)

 

Write a program that takes a file as input and creates an output file that is an exact copy. The names of the files MUST come from the command line. If they are NOT there, end the program with an appropriate message.

 

Important points:

       The file may contain any type of data.

       Do NOT open the output file if the input file did not open properly.

       You do NOT need to have more than the main function.

       You do NOT need to include any comments.

 


3.     2-dimensional Arrays (You may hardcode all array dimensions.):

 

       Define a 2-dimensional array of integers named values with 10 rows and 10 columns. (5 points)

 

 

 

 

       Assume that the array has been loaded with values. Write code (use for loops) to define and compute two values, namely the sum of the elements on the main-diagonal and the sum of the elements on the off-diagonal. The main-diagonal contains those elements from the upper left to lower right (shown with *'s). The off-diagonal contains those elements from the upper-right to the lower-left (shown with $'s). (10 points)

 

 

 

 

 

 

 


       A 2-dimensional array is symmetric if for all i and j, the array entry in row i, column j is equal to the entry in row j, column i. (In C++, this would be expressed by the condition (values[i][j] == values[j][i]).)

 

Write a Boolean function called isSymmetric that takes the 10 10 array defined on the previous page as a parameter and returns true if it is symmetric and false otherwise. (10 points)


4.     Recursion:

 

       Write a recursive function named comp that takes an integer greater than or equal to 0 and returns an integer, given the following base and general cases. (10 points)

Base case: comp(0) = 1

General case: comp(n) = 2 * comp(n-1) + 3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

       What would be returned by the function call comp(3)? (3 points)

 

 

 


Consider the following C++ code.

 

int mystery(int i, int j){

if (i > j)

return 0;

else

return (i + mystery(i + 1, j));

}

 

       What would be returned by the following function calls? (3 points each)

 

mystery(3,2)

 

mystery(3,3)

 

mystery(2,3)

 

       In English, describe what the call mystery(x, y) returns given values for x and y, where x is greater than y. HINT: You may want to try more values than the ones given above. (3 points)