The University of Alabama in Huntsville

Electrical & Computer Engineering

CPE 197 01

Spring 2001

Sample Final Exam

April 23, 2001

 

 

1) (4 points) Add type casts to the following statements to make the type conversions clear and explicit. Your answers should produce the same results as the original statements.

                a. someInt = someFloat + 5 / 2.5 + someInt;

                b. someFloat = 2.5 * someInt / someFloat;

 

 

 

 

2) (3 points) Write the function heading for a void function named PrintMax that accepts a pair of integers and prints out the greater of the two. Document the data flow of each parameter with /*in*/, /*out*/, or /*inout*/.

 

 

 

 

3) (8 points) What is the output of the following program segment? (All variables are of type int.)

 

                i = 1;

      while (i <= 5)

      {

         sum = 0;

         j = 1;

         while (j <= i)

         {

            sum = sum + j;

            j++;

         }

         cout << sum << ' ';

         i++;

      }

 

 

4) (10 points) Write a while loop that copies all the characters (except tab ‘\t’ characters) from an input file stream inFile to an output file stream outFile. The loop should terminate when end-of-file is detected. Make all appropriate declarations and function calls.

 

 

 


5) (3 points) What is the output of the following program segment?

void one(void)

{

   int a = 10;

   cout << “ a = ” << a << endl;

}

void two (void)

{

   int a = 0;

   one( );

   cout << “ a = ” << a << endl;

}                                                                 output: ______________________________

int main (void)

{

   int a = 20;

   two( );

}

 

 

6) (8 points) What is printed by the following program fragment? (All variables are of type int.)

 

                for (i = 4); i >= 1; i--)

      {

         for (j = 0; j < i; j--)

            cout << j << “***”;

         cout << i << endl;

      }

 

 

7) (3 points) What is the output of the following program segment?

 

void one (int x)

{   x+=1;

   cout << “x = ” << x << endl;

}

int two (int x)

{

   int one = 10, y = 20;

   one-=1;

   one(y);

}                                                                           output: ______________________________

int main (void)

{

   int a = 1, b = 2;

   two(b);

}

 

8) (3 points) Declare an array called array_1 that contains 10 character elements.

 

 

 

 

 

 

 


9) (12 points) Given the following program segment, what is the output of each cout statement?

                                                double x[5];

                  int y;

 

                  for (y=0; y<5; y++)

                      x[y] = y*3;

                  y = 3;

 

a) cout <<  x[4] << endl ;                             ______________________________

b) cout << x[0] << endl;                                   ______________________________

c) cout << x[1] + 2.0 << endl;                           ______________________________

d) cout << x[(1+1)*2] << endl;                   ______________________________

e) cout << x[(int)x[1]] << endl      ;                ______________________________

f) cout << x[++y] << endl;                   ______________________________                     

 

 

10) (11 points) a) (7 points) Write a user function called init_array that initializes any one-dimensional array of integers to contain all zeros.  The parameters are the array itself and its number of elements. b) (4 points) Show all necessary declarations and the calling statement in the calling function for the above user function. The array name is array_1 and its size is 20.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

11) (5 points) a. (4 points) Use correct C++ syntax to define a new type called player that consists of an integer called assists, an integer called points, and an integer called rebounds. b. (1 point) Declare a variable called The_Answer of the type in question 22a.

 

 

 

 

 

 

 

 

 

 

 

 

 

 


12) (10 points) a. (7 points) Write a user function called triple_double to determine whether a player achieves a triple-double during a game. (i.e. check if there are >=10 assists, AND >=10 points, AND >=10 rebounds.)  The function should return one for a triple-double and zero otherwise.  Use the type player_t described in problem 22. b. (3 points) Show all the necessary declarations and the calling statement in the calling function for the user function in question 23a.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

13) (10 points) Write a value-returning function named Min that returns the smallest of its three integer parameters.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14) (5 points) Given these values for Boolean variables x, y, and z: x = true, y = true, and z = false, indicate whether each expression is true (T) or false (F).

 

a. ____ (y || z) || !x

b. ___ z || x || y

c. ___ y || (z && !x)              

d. ___ z || (x && !(y || z))

e. ___ x || !x && z

 

 

15) (5 points) Describe the problem inputs, outputs, and algorithm for this problem:  Given the three coefficients of a quadratic polynomial, write the two floating-point solutions to the screen, echo printing the inputs.