Name ___________________
CPTR246 Spring '07 (100
total points)
Exam 1
Classes and Objects
1. We’re going to develop a class to help out Jazzman’s. Below is the definition for a class called Drink. The public and private sections of the class declaration are complete. You are to fill in the code for each member function, using the coded comments, along with your own experience in coding classes, as a guide to each function's use. (4 points for each function)
/*************************************************
* Drink.h *
*************************************************/
#include <string>
class Drink{
public:
Drink(string newDrink, char whichCup,
string newRecipe);
// .... see below
string setPrices();
// .... see below
string getRecipe();
// .... see below
void raisePrices(double increase);
// .... see below
string affordable(double cash);
// .... see below
private:
string name; // name of this drink
char cup; // which cup should be used (‘H’
or ‘C’)
string recipe; // details of how to make this drink
double cost[3]; // cost for tall, grande, and supreme sizes
};
Drink::Drink(int string
newDrink, char whichCup, string newRecipe){
// Intent: Constructor
// Pre: name, cup, and recipe are provided as
parameter
// Post: data members initialized properly
// NOTE:
new drinks are initially FREE !!!
}
string Drink::setPrices(double
tall, double grande, double supreme){
// Intent: To set the prices
of this drink
// Pre: none
// Post: data members are
updated appropriately
// NOTE:
array ‘cost’ contains tall, grande, and supreme prices
// in that order
}
string Drink::getRecipe(){
// Intent: This is an
accessor function that simply returns the drink’s recipe.
// Pre: none
// Post: returns the drink’s
recipe
}
void Drink::raisePrices(double
increase){
// Intent: to raise the
prices for this beverage
// Pre: parameter ‘increase’
is a percentage between 0 and 100 <----NOTE!
// Post: data members
updated properly
// FOR FULL CREDIT, USE A for
}
string Drink::affordable (double
cash){
// Intent: determine the
largest size drink that can be bought with ‘cash’
// pre: none
// post: return the size
(“tall”, “grande”, “supreme”, or “none”) that can
// be purchased with the parameter ‘cash’
//
// NOTE:
array ‘cost’ contains tall, grande, and supreme prices
// in that order, and prices
increase from tall to supreme
}
2. Using the class definition from question 1, give C++ code (1 line) to define a Drink object called topSeller with the name “plain coffee” that used a hot cup with the recipe “None-just give them the empty cup.” (4 points)
3. Using the topSeller object that you defined in question 2 and the affordable method from the class definition in question 1, write code that will display on the screen the largest size of topSeller that you can purchase with $2.00. (4 points)
4. Using the class definition from question 1, give C++ code (1 line) to define an array called menu of 20 drinks. (4 points)
5. Assuming that the array menu defined in question 4 has been loaded with 20 drinks and all of their information, write a loop that increases the prices of all 20 drinks by 10%. (4 points)
Recursion
6. Write a recursive function named guessMe that takes one parameter, an integer greater than or equal to 1 and returns an integer, given the following base and general cases. (8 points)
Base case: guessMe(1)
= 1
General
case: guessMe(n)
= 2 * guessMe(n-1) + 4
What would be returned by the function call guessMe(4)? (5 points)
Call-by-value and
Call-by-reference
7. What is the output of the following C++ code? Write the output below the program. (10 points)
// sample
program for exam # 1
#include
<iostream>
using
namespace std;
void callMeFirst(int
a, int b){
a
= 3;
b = 4;
cout << a << " "
<< b << endl;
}
void callMeNext(int
& a, int & b){
a = 5;
b = 6;
cout << a << " "
<< b << endl;
}
int main() {
int num1 = 1;
int num2 = 2;
cout << num1 << "
" << num2 << endl;
callMeFirst (num1, num2);
cout << num1 << "
" << num2 << endl;
callMeNext (num1, num2);
cout << num1 << "
" << num2 << endl;
return 0;
}
Input Buffers and
8. Below is a program that we looked at in class to learn about input buffers and cin’s member functions. Answer the questions below the code:
#include
<iostream>
using
namespace std;
int
main() {
cout
<< "Enter an integer: (ctrl-d to end)" << endl;
int x; //
holds integer read
cin >> x;
while (!cin.eof()){
while (cin.fail() || cin.peek() !=
'\n'){
cout << "That was
not an integer. Try again: ";
cin.clear();
cin.ignore(80, '\n');
cin >> x;
}
cout << "That was a
" << x << endl;
cout << "Enter another integer
(ctrl-d to end): ";
cin >> x;
}
cout << endl;
return 0;
}
a. Matching: Select the description that best matches each of the following member function calls if it would be executed after the following code: (2 points each)
int x; //
suppose you’re reading in an integer
cout << “Enter an
integer: “;
cin >> x;
a)
cin.clear();
b)
cin.eof()
c) cin.fail();
d) cin.ignore(80, ‘\n’);
e)
cin.peek();
_____ returns true if the user hits the key combination ctrl-d
_____ returns true if the user enters all letters
_____ allows us to look at the first character (byte) in the buffer
_____ turns off the fail and eof flags
_____ removes characters (bytes) from the buffer
b. Give an example of what the user might enter in the above example that would cause the condition (cin.fail()) to be false and the condition (cin.peek() != '\n') to be true. (2 points)
Random Number Generation
9. Below is the prototype for the built-in function that generates a random number:
int rand(); // returns an integer between 0 and RAND_MAX
Complete the three assignment statements below so that variables x, y, and z have the values described in the comments. (Recall: the C++ remainder operator is % and the build-in value RAND_MAX.) (9 points)
int x, y;
double z;
x = // a
random integer between 0 and 9
y = // a random integer between 1 and 12
z = // a random real number between 0 and 1
2-dimensional Arrays
10. Write the specified code in C++:
· Define a 2-dimensional array of integers named numbers with 7 rows and 7 columns. (5 points)
· Assume that the array numbers has been filled with integers. Write code that determines how many negative numbers are in the array and then displays it on the screen (5 points)
11. A 2-dimensional array is called symmetric if for all i and j, the array entry in row i, column j is equal to the array entry in row j, column i.
Write a function called isSymmetric that takes two parameters, a two-dimensional array of integers with 7 columns and an integer for the number of rows. It returns a boolean, specifically true if the array is symmetric and false otherwise. (10 points)