Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Solutions to Sample Midterm Exam Questions - Spring 2009

1. Terms and definitions

a) What are the three structures of structured programming?
Sequence

Selection

Repetition

b) What is a post-test loop?

A loop in which the exoression is tested after the loop body.

c) What are the values a bool variable can have?

true, false

d) What is a variable?

A variable is a location in memory where a value can be stored for use by a program.

e) True or false?  Every C++ program must have a main() function.___TRUE_______
 

2. Describe the exact output that is produced by the following program segments. Use an underscore (_) to indicate blank spaces.

          double PI = 3.14159;
     int NUM = 73;
     cout.setf(ios::fixed);
     cout.setf(ios::showpoint);
     cout.precision(3);
     cout << "NUM=" << setw(6) << NUM
          << "\nPI=" << PI << endl;

 

NUM=_ _ _ _73
PI=3.142


3. Write the statements, including a loop, required to calculate y(t) from the equation:

            -3t2 + 5       t >= 0
    y(t) = 
             3t2 + 5       t < 0


for values of t between -9 and 9 in steps of 3.  Display each value of t and y(t).

You do not need to write a complete program but you should include declaration statements for all the variables that you use.

  double t, y, increment;
   
  t = -9.0;

  increment = 3.0;

  while( t <= 9.0)
  {
    if( t >= 0.0 )
      y = -3.0*t*t + 5.0;
    else
      y = 3.0*t*t + 5.0;

    cout << t << "  " << y << endl;

    t += increment;

  }


4. A program is needed to score a dice game as follows.  If the roll of a pair of dice is 7 or 11, the player's winnings are to be increased by 100.  If the roll turns up any other number, the winnings are to be reduced by 100. Write the C++ statement or statements that will score the dice game.  Assume that the following declarations have been made and that a value has been assigned to variable roll.

     int roll; // the value of the dice roll
    double winnings; // the player's score 

        if( roll == 7 || roll == 11 )
        winnings +=100.0;
      else
        winnings -= 100;

5.  Relational and Logical Expressions.

a) Assuming the value of variable count is 0 and the value of variable limit is 10, state whether each the following expressions is true or false.

    1. (count == 0) && (limit > 12) ________ false _______________

    2. (limit > 20) || (count < 5)    _______ true ____________________

    3. !(count == 12)            _______ true____________________                                                       

b) In algebra we see numeric intervals given as

    2 < x < 3

This is not how we express this relationship in C++, however.  Give the correct C++ boolean expression that specifies that x lies between 2 and 3.

(2 < x) && (x < 3)


6. The price of gasoline has fluctuated widely in recent months causing concern for motorists. Here you are to write a complete C++ program to calculate the cost of a visit to a gas station. Your program should ask the user whether he or she wants regular ($2.129 per gallon) or premium ($2.279 per gallon) gasoline, whether or not a car wash ($5.99) is desired, and if a receipt should be printed. Of course the program must also ask how many gallons it took to fill the tank. Then the program should calculate the cost of the gas and the total cost including a car wash if there was one. If the user wanted a receipt, display the number of gallons, the cost of the gasoline, any car wash cost, and the total cost as in this example:


Gallons = 10.00
Gasoline = $21.29
Car wash = $5.99
Total cost = $27.28

Notes:

  • Use variables to hold constant values (e.g., 2.129).

  • Display two digits after the decimal point.

  • Include a few comment statements to document the program.


// Gas Pump Cost
#include<iostream>                      
using namespace std;

int main()
{
  // declarations                       

  double regular = 2.129, premium = 2.279, wash_cost = 5.99;
  double gas_total, total, gallons;
  char gas_type, receipt, wash;

  // input                                   

  cout << "Type of gas? r - regular, p - premium: ";
  cin >> gas_type;
 
  cout << "Car wash? y or n: ";
  cin >> wash;

  cout << "Receipt? y or n: ";
  cin >> receipt;

  cout << "Enter the number of gallons: ";
  cin >> gallons;

  // cost of gas                           
  if(gas_type == 'r')
    gas_total = regular * gallons;
  else
    gas_total = premium * gallons;

  // total cost                             
  total = gas_total;

  if(wash == 'y')
    total = total + wash_cost;
  
  // set format flags and precision         
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);

  // receipt                               
  if(receipt == 'y')
  {
    cout << "Gallons = " << gallons
         << "\nGas = $" << gas_total;
  
    if(wash == 'y')
       cout << "\nCar wash = $" << wash_cost;

     cout << "\nTotal = $" << total << endl;
  }

  return 0;                             
}





 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming