Name ___________________

 

CPTR246 Spring '01 (100 total points) Exam 2

 

1.      Template Functions: (20 points)

 

Write a template function named average that takes two parameters, the first of which is an array (you should know what the second would be), and returns the average of the values in the array. Examples of its usage would be:

 

int values[4] = {50, 75, 98, 78};

double numbers[3] = {0.1, 0.2, 0.3};

cout << average(values, 4) << endl; // would display 75.25

cout << average(numbers, 3) << endl; // would display 0.2

 

 


  1. Command Line Arguments: (25 points)

 

Write a program that takes a file of integers as input. The name of the file must be on the command line followed by an option flag, either d or t, indicating whether the integers in the file are to be displayed on the screen (-d option), or if the total of the integers is to be displayed (-t option). If these are NOT in the command line, end the program with an appropriate message. Example runs:

 

fileprinter.exe myfile.dat d (would list the integers on the file)

fileprinter.exe myfile.dat t (would print just the total of them)

 

Important points:

        The program should terminate if it encounters anything on the file other than an integer.

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

        You do NOT need to include any comments.

 

The next page is blank for the solution to this problem, if needed.



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

 

        Define a 2-dimensional array of characters named chessboard with 8 rows and 8 columns. (5 points)

 

 

 

 

        Give code that will load this array with spaces. (5 points)

 

 

 

 

 

 

 

 

        Write a function called queenTakes that takes three parameters, the 8 by 8 array defined above and two integers between 0 and 7. It returns nothing. The two integers represent the row and column location of a Queen in chess. The function is to place asterisks in all locations that can be captured by the queen (that is, all locations in that row, that column, and along the diagonals that pass through that position) and a Q in that location. (20 points)

 

The following examples will result in the chessboards shown:

queenTakes(chessboard, 2, 3) queenTakes(chessboard, 0, 0)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Hint: Use 6 loops one for the row of *s, one for the column, and one for each diagonal from the Queen. Then put in the Q for the Queen.

 

The next page is blank for the solution to this problem.


 


  1. 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) = 2

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

 

 


5.      Write a recursive function called displayBackwards to display, in reverse order, the contents of a file that contains all integers. For example, if the file contained 10 20 30, then the program would display 30 20 10. The last integer on the file is displayed first, the next-to-last is displayed next, and so on until the first integer on the file is displayed. It has one parameter, the input file stream object, already opened. A sample call to the function would be as follows: (10 points)

 

ifstream inFile(mydata.dat);

displayBackwards(inFile);