Name ___________________
CPTR246 Spring '04 (100
total points)
Exam 1
Classes and Objects
1. Below is the definition for a class called Student. 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)
/*************************************************
* Student.h
*
*************************************************/
#include <string>
#ifndef _STUDENT_H
#define _STUDENT_H
class Student{
public:
Student(int newId, string newName);
// .... see below
string Name();
// .... see below
void PostExam(int examNumber, int testScore);
// .... see below
double Average();
// .... see below
bool DidPass();
// .... see below
private:
int id; // student id
string name; // student name
int scores[4]; // scores on 4 exams
};
Student:: Student (int
newId, string newName){
// Intent: Constructor
// Pre: student’s id and name provided as parameter
// Post: data members initialized properly
}
string Student:: Name(){
// Intent: This is an
accessor function that simply returns the student’s name.
// Pre: none
// Post: returns the
student’s name
}
void Student:: PostExam(int examNumber, int testScore){
// Intent: To record an exam
grade.
// Pre: examNumber is a
value between 1 and 4 <---- NOTE!
// testScore is the grade on the exam.
// Post: data members
updated properly
}
double Student::Average (){
// Intent: to return the student’s
average
// pre: assume that all four
exam grades have been entered
// post: returns the average
of the four exam grades as a real number
}
bool Sorority::DidPass (){
// Intent: a Boolean
function to determine if the student passes
// pre: assume that all four
exam grades have been entered
// post: returns true if the
student’s average is >= 60.0, false otherwise
// For full credit, use the Average function as
a support function
}
#endif
2. Using the class definition from question 1, give C++ code (1 line) to define a Student object called me with you as the student. (3 points)
3. Using the class definition from question 1, give C++ code (1 line) to define an array called class of 8 students. (3 points)
4. Assuming that the array defined in question 3 has been loaded with students and all of their grades, write a for loop that displays the students’ names and averages on the screen. (3 points)
Recursion
5. Write a recursive function named guessMe that takes one parameter, an integer greater than or equal to 0 and returns an integer, given the following base and general cases. (10 points)
Base case: guessMe(0)
= 3
General
case: guessMe(n)
= 3 * guessMe(n-1) + 2
What would be returned by the function call guessMe(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. (10 points)
// sample
program for exam # 1
#include
<iostream>
using
namespace std;
void fun1(int
& a, int b){
a = 10;
b = 20;
cout << a << " "
<< b << endl;
}
void fun2(int
a, int & b){
a = 20;
b = 18;
cout << a << " "
<< b << endl;
}
int main() {
int num1 = 4;
int num2 = 5;
cout << num1 << "
" << num2 << endl;
fun1 (num1, num2);
cout << num1 << "
" << num2 << endl;
fun2 (num1, num2);
cout << num1 << "
" << num2 << endl;
return 0;
}
Input Buffers and
7. Below is a program that we looked at in class to learn about input buffers and cin’s member functions. Answer the questions below regarding the various member functions: (3 points each)
#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;
}
What would
cause cin.eof() to be
true?
What would cause cin.fail()
to be true?
What does the combination of
cin.clear() and cin.ignore(80, ‘\n’) do?
Random Number Generation
8. 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 %.) (6 points)
int x, y, z;
x = // a random # between 0 and RAND_MAX
y = // a random # between 0 and 5
z = // a random # between -3 and 3
2-dimensional Arrays
9. Write the specified code in C++:
· Define a 2-dimensional array of integers named scores with 8 rows and 4 columns. (5 points)
· Assume that the array scores has been filled with integers. Write code that will count the number of integers in the array that are less than 60 and display it on the screen. (5 points)
Binary Search

Arrays and Functions
11. Write a function named nullRow that takes two parameters, a two-dimensional array of integers (the array has 10 columns) and an integer for the number of rows in the array. The function is to return true if at least one row of the array is all zeros, false otherwise. Therefore, the function returns a Boolean. It does not print anything. (15 points)