CSCI 159: Fall 2026 Practice Midterm Sample Solutions
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):
- A definition for a real number variable named diameter that is initialized to the value 32.1.
Sample solution
double diameter = 32.1;
|
- A definition for an integer constant named BaseYear that is initialized to the value 2000.
Sample solution
const int BaseYear = 2000;
|
- A definition for a character variable named command, that is initialized to the character '?'.
Sample solution
char command = '?';
|
- A statement using cout to display the product of numeric variables x and y.
(Assume x and y have been previously declared and initialized.)
Sample solution
cout << (x * y);
|
- A statement using cin to read a user input into a variable named firstval.
(Assume firstval has been previously declared and initialized.)
Sample solution
cin >> firstval;
|
- 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.)
Sample solution
compdata = sqrt(compdata);
|
- 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.)
Sample solution
cout << fixed << setprecision(2) << setw(20) << A << setw(20) << B;
|
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;
}
Sample solution
within double compute(int &circsUsed, double diam, double scale)
circsUsed is a pass-by-reference parameter (parameters are always local)
diam, scale are pass-by-value parameters (as above)
result is a local variable
Pi is a global constant (used here inside the calculation)
DefaultCircs is a global variable (used here inside the assignment statement)
(the 2.0's are literal values)
|
(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.
Sample solution
Enter the circle diameter: 2
The final values are 3.14, 2, 10
Explanation:
(the 2 on the first line is actually the user typing)
circs is 0 when passed by reference to the function (used in the function as circsUsed),
which immediately sets it to DefaultCircs, i.e. 10
the value 2 is passed to diam and the value 1 is passed to scale
the computation is thus
1 * 3.14 * (2/2) * (2/2)
i.e. 1 * 3.14 * 1 * 1
so the computed/returned value is simply 3.14
thus in the final cout we see 10 3.14 for the result, 2 for the entered diameter,
and 10 for the altered circs
|
Question 3: Write a program [10]
Write a complete and correct C++ program that does the following:
- Prompts the user to enter two real-number values.
- Reads the two values the user enters, storing them in appropriate variables.
- If the second value is negative it is replaced with its positive equivalent (e.g. if x was
negative then replace it with -x).
- Uses the pow routine (from cmath) to compute the first raised to the power of the second,
and stores the computed value in another variable. (E.g. if they entered 3.0 and 2.0 then it would compute and store 3 squared, i.e. 9.0.)
- Displays the original two entered values and the computed result, formatted so that each
is shown with exactly 2 digits after the decimal point.
Sample solution
#include <iostream>
#include <cmath>
int main()
{
float firstVal, secondVal, result;
std::cout << "Please enter two real numbers: ";
std::cin >> firstVal >> secondVal;
if (secondVal < 0) {
secondVal = - secondVal;
}
result = pow(firstVal, secondVal);
std::cout << std::fixed << setprecision(2);
std::cout << firstVal << " to the power of " << secondVal << " is " << result << std::endl;
}
|
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)
Sample solution
double getNumber(double min, double max)
{
double userVal;
cout << "Please enter a real number in the range " << min << " to " << max << ":";
cin >> userVal;
if (cin.fail()) {
cout << "That was not a number, please try again" << endl;
cin.clear();
cin.ignore(80, '\n');
userVal = getNumber(min, max); // or: return getNumber(min, max);
} else if ((userVal < min) || (userVal > max)) {
cout << "That was not in the requested range, please try again" << endl;
userVal = getNumber(min, max); // or: return getNumber(min, max);
}
return userVal;
}
|
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)
{
Sample solution
void showTimesTable(int base, int numentries)
{
cout << "Displaying " << numentries << " multiples of " << base << endl;
int current = base;
for (int count = 1; count <= numentries; count++) {
cout << current << endl;
current = current + base;
}
}
|