Name ___________________
CPTR246 Spring 2011 (100
total points)
Exam 1
Classes and Objects
1. We’re going to develop a class to help us keep track of our DVD collections. Below is the definition for a class called Collection. 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)
/*************************************************
* Collection.h *
*************************************************/
#include <string>
class Collection{
public:
Collection(string name);
// .... see below
void addDVD(string title);
// .... see below
void display();
// .... see below
string getFavorite();
// .... see below
bool setFavorite(string title);
// .... see below
private:
string owner; // DVD collection owner’s name
int numberOfDVDs; // number of DVDs in the person’s collection
string DVD[500]; //
array containing the title for up to 500 DVDs
int favorite; // the index of this person’s favorite DVD,
// -1
if the owner has no favorite
};
Collection:: Collection (string
name){
// Intent: Constructor
// Pre: name is the name of the person for whom this
collection is being created
// Post: data members initialized properly
// NOTE:
when created, the DVD collection has no items in it (and so there
// can be no favorite)
}
void Collection::addDVD(string
title){
// Intent: To add a DVD to
the collection.
// Pre: collection has fewer
than 500 DVDs in it and ‘title’ is not already in
// the collection
// Post: instance variables
are updated properly
}
void Collection::display(){
// Intent: display a list of
all of the DVDs in the collection
// Pre: none
// Post: none
}
string Collection::getFavorite(){
// Intent: This is an
accessor function that returns the title of the
// the owner’s favorite DVD in the
collection
// Pre: none
// Post: The title of the
favorite DVD is returned, if the owner has one;
// returns “No favorite selected” otherwise
}
bool Collection::setFavorite(string
title){
// Intent: to record that
the collection’s owner has a new favorite DVD
// pre: none
// post: if the title is in
the collection, ‘favorite’ is set to the index
// of that title and true is returned. If the title is not in the
// collection, then return false
}
2. Using the class definition from question 1, give C++ code (1 line) to define a Collection object called mine with the owner’s name as your name. (3 points)
3. Using the mine object that you defined in question 2 and method definitions from the class definition in question 1, write code that will add two movies to your collection and then display the collection. (4 points)
Verifying input
4. Write code that will prompt the user for an integer between 1 and 10, gets their response, and then checks to make sure that they did indeed enter an integer between 1 and 10. If they did not, display an appropriate message on the screen and allow the user to try again. Use a loop so that the program will not continue until the user enters an appropriate value. (6 points)
int n; // should be an integer between 1
and 10
Recursion
5. Write a recursive function named gizmo that takes one parameter, an integer greater than or equal to 0 and returns an integer, given the following base and general cases. (8 points)
Base case: gizmo(0)
= 3
General
case: gizmo(n)
= (1 + gizmo(n-1)) * 2
What would be returned by the function call gizmo(3)? (5 points)
Call-by-value and
Call-by-reference
6. What is the output of the following C++ code? Write the output below the program. (6 points)
// sample
program for exam # 1
#include
<iostream>
using
namespace std;
void someFunction(int
& a, int & b){
cout << a << " "
<< b << endl;
a = 30;
b = 40;
cout << a << " "
<< b << endl;
}
int main() {
int num1 = 10;
int num2 = 20;
someFunction (num1, num2);
cout << num1 << "
" << num2 << endl;
return 0;
}
File I/O
7. Make the necessary changes to the following code so that the input comes from the file values.dat rather than from the keyboard. (6 points)
#include
<iostream>
using
namespace std;
int main() {
cout
<< "Enter integers: (ctrl-d to end)" << endl;
int x;
int
count = 0;
int
total = 0;
cin >> x;
while (!cin.eof()){
count = count + 1;
total = total + x
cin >> x;
}
if (count > 0)
cout << “The average is “ <<
(double) total / count << endl;
else
cout << “No integers were entered”
<< endl;
return 0;
}
Input Buffers and I/O
States
8. 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();
_____ and _____ return true if the user hits the key combination ctrl-d
_____ 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
a. Give an example of what the user might enter in the above code that would cause the condition (cin.fail())to be true and (cin.eof())to be false. (2 points)
b. Give an example of what the user might enter in the above code that would cause the condition cin.fail() to be false but the condition (cin.peek() != '\n') to be true. (2 points)
c. Suppose that when the code above is excuted, the user entered “12,3458” (note that 8 signifies that the user hit the enter key and that the double quotes where not actually entered by the user).
a. What would the variable x contain? (1 point)
b. What, if anything, remains in the buffer? (1 point)
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 built-in value RAND_MAX.) (6 points)
int x, y;
double z;
x = // a random integer
between 0 and 9
y = // a random integer
between 1 and 10
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 matrix with 25 rows and 10
columns. (5 points)
·
Assume that the array matrix has been defined. Write code that will set every
element in the array matrix to -1
(negative one). (5 points)
11.
Write a function called numberNegative that takes two
parameters, a two-dimensional array of integers with 10 columns and an
integer for the number of rows. It
returns an integer, specifically the number of entries in the array
that are negative (strictly less than zero). (10 points)