CSCI 159: Fall 2026 Practice Midterm
Sections F26N01-F26N02 (Dr. Wessels)

Question   Mark   Possible
1. Short answer   /10
2. Scopes and functions   /10
3. Write a program   /10
4. Write a recursive function   /10
5. Loops   /10
TOTAL   /50

Question 1: Short answer [10]

Provide valid single-line C++ definitions or statements for each of the following (assume it is within an otherwise-correct main routine, and that all the needed #includes and using statements were already provided):

  1. A definition for a real number variable named diameter that is initialized to the value 32.1.
    
    
    
    

  2. A definition for an integer constant named BaseYear that is initialized to the value 2000.
    
    
    
    

  3. A definition for a character variable named command, that is initialized to the character '?'.
    
    
    
    

  4. A statement using cout to display the product of numeric variables x and y.
    (Assume x and y have been previously declared and initialized.)
    
    
    
    

  5. A statement using cin to read a user input into a variable named firstval.
    (Assume firstval has been previously declared and initialized.)
    
    
    
    

  6. A statement that replaces the value stored in a numeric variable compdata with the square root of its current value. E.g. if compdata was 16.0 then its new value would be 4.0. (Assume compdata has been previously declared and initialized and that the cmath library has been included, so sqrt is available.)
    
    
    
    

  7. A statement using cout, setw, fixed, and setprecision to display two real-number variables named A and B in columns of width 20 and with exactly 2 digits of precision after the decimal point.
    (Assume A and B have been previously declared and initialized.)

Question 2: Scopes and functions [10]

(i) For the compute function in the C++ program below, identify each variable, constant, and parameter (clearly labeling them as variable, constant, or parameter) and for each of them clearly identify whether they are global or local and which of the parameters are pass-by-reference.
#include <iostream>

int DefaultCircs = 10;
const double Pi = 3.14;

double compute(int &circsUsed, double diam, double scale);

int main()
{
   int circs = 0;
   double d;
   std::cout << "Enter the circle diameter: ";
   std::cin >> d;
   double totArea = compute(circs, d, 1.0);
   std::cout << "The final values are ";
   std::cout << totArea << ", " << d << ", " << circs << std::endl;
}


double compute(int &circsUsed, double diam, double scale)
{

   circsUsed = DefaultCircs;

   float result = scale * Pi * (diam / 2.0) * (diam/2.0);

   return result;

}

(ii) For the program above, show as precisely as possible the output that would be produced if the compiled program was run and the user entered the value 2.0.

Question 3: Write a program [10]

Write a complete and correct C++ program that does the following:

(extra space for question 3 if needed)

Question 4: Write a recursive function [10]

Provide a correct recursive (no loops) implementation of the C++ function described by the comments and profile given below. (Assume the rest of the C++ program using this function is already written and correct, including all the appropriate #includes and usings: do NOT write any code except the function itself.)
// getNumber
//    prompts the user to enter a real number in the range min..max (inclusive)
//    reads their response and checks it for validity (a number that is in the given range),
//        repeating (recursively) until a valid response is provided
//    returns the valid response obtained
double getNumber(double min, double max)

Question 5: Loops [10]

The function below has been implemented using a while loop. Rewrite it to use an appropriate for loop instead, keeping the overall logic and behaviour as close to the original as possible.
// ORIGINAL
void showTimesTable(int base, int numentries)
{
    cout << "Displaying " << numentries << " multiples of " << base << endl;
    int current = base;
    int count = 1;
    while (count <= numentries) {
       cout << current << endl;
       count++;
       current = current + base;
    }
}


// YOUR RE-WRITE
void showTimesTable(int base, int numentries)
{