Name  ___________________________

 

CPTR246  Spring '12 (100 total points)                                                                 Exam 1

 

Classes and Objects

1.      Below is the definition for a class called Course.  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)

 

/*************************************************

*    Course.h                                    *

*************************************************/

#include <string>

 

#ifndef _COURSE_H

#define _COURSE_H

 

class Course{

  public:

    Course (string courseName, string courseInstructor);

         // .... see below

    void EnrollAStudent(int studentID);

         // .... see below

    int NewInstructor(string newProf);

         // .... see below

    void DisplayCourseRoster();

         // .... see below

  private:

    string name;            // course name

    string instructor;     // name of the instructor

    int current;            // total number of students enrolled

    int IDs[100];           // ID numbers of the students enrolled

};

 

 

Course::Course (string courseName, string courseInstructor){

// Intent:  Constructor

// Pre:   course and instructor names are provided as parameter

// Post:  data members initialized properly

 

 

 

 

 

 

 

 

 

 

 

 

 

}


void Course::EnrollAStudent(int studentID){

// Intent: To add student to the course.

// Pre: studentId is an integer identifying the student.

// Post: data members updated properly

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}

void Course::NewInstructor(string newProf){

// Intent: to change the course’s instructor

// pre: none

// post: returns nothing

 

 

 

 

 

 

 

 

 

 

 

 

 

}

 

void Course::DisplayCourseRoster (){

// Intent: used to display the students’ ID numbers, one per line

// pre: none

// post: students’ IDs are displayed on the screen

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}

#endif


2.      Using the class definition from question 1, give C++ code to define a Course object, initialized to have the name "CPTR 246" and the instructor "Peluso".  (4 points)

 

 

 

 

 

 

 

 

3.      Using the class definition from question 1, give C++ code (only two statements are necessary!)  that will (1) add yourself to the course (make up an ID#) defined in question 2 and (2) display the course roster on the screen. (4 points)

 

 

 

 

 

 

 

 

 

 

Call-by-value and Call-by-reference

4.      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 mystery(int fred, int & ethel){

        cout << fred << " " << ethel << endl;

        fred = 100;

        ethel = 200;

        cout << fred << " " << ethel << endl;

}

 

int main() {

        int value1 = 50;

        int value2 = 75;

        mystery(value1, value2);

        cout << value1 << " " << value2 << endl;

        return 0;

}

 

 


Input Buffers and I/O States

5.      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:

 

1    #include <iostream>

2    using namespace std;

3   

4    int main() {

5    cout << "Enter an integer: (ctrl-d to end)" << endl;

6     int x;            // holds integer read

7     cin >> x;        

8     while (!cin.eof()){

9           while (cin.fail() || cin.peek() != '\n'){

10                cout << "That was not an integer.  Try again: ";

11                cin.clear();

12                cin.ignore(80, '\n');

13                cin >> x;

14          }

15          cout << "That was a " << x << endl;

16          cout << "Enter another integer (ctrl-d to end): ";

17           cin >> x;

18    }

19    cout << endl;    

20    return 0;

21   }

 

 

a.       What would the user enter (on line 7, 13, or 17) in the above example that would cause the condition (cin.eof()) (on line 8) to be true.  (2 points)

 

 

b.      Give an example of what the user might enter (on line 7, 13, or 17) in the above example 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 program reached line 7, the user entered “12348” (note that 8  signifies that the user hit the enter key, and that the double quotes where not actually entered by the user).  What, if anything, remains in the buffer?  (2 points)

 

 

d.      Suppose that when the program reached line 7, the user entered “12.348” (note that 8  signifies that the user hit the enter key and that the double quotes where not actually entered by the user).  What, if anything, remains in the buffer?  (2 points)

 

 

e.       What is the purpose of  cin.clear() on line 11 of the program? (3 points)

 

 

 

f.       What is the purpose of cin.ignore(80, '\n') on line 12 of the program?  (3 points)

 

 


File I/O

6.      Make the necessary changes to the following code so that the input comes from a file rather than from the keyboard.  The program is to ask the user for the name of the file.  (10 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;

}

 


Formatting Output

7.      Given the following variable definitions, give cout statements that would provide the described formatting.

 

#include <iomanip>

. . .

int a;

int b;

double average;

 

a.       Display integers a and b on the same line with a left-justified in a column of width 8 and b right justified in a column of width 12.  (2 points)

 

 

 

 

 

 

b.      Display the double average to 8 significant digits. (2 points)

 

 

 

 

 

 

c.       Display the double average, forcing it to print in non-scientific notation with 3 decimal positions. (2 points)

 

 

 

 

 

 

 

 

True or False

8.      For each of the following, indicate whether the statement is True or False by circling a T or an F, respectively. (8 points)

 

T    F     A C++ program that uses cin for input can get its input from a file instead using redirection < on the command line.

 

T    F     When displaying error messages to the user, it is better to user cerr  than cout because cout can be redirected to a file and cerr cannot.

 

T    F     In C++, if a program tries to reference an element outside of an array, it will cause an ArrayOutOfBoundsException just as it does in Java.

 

T    F     In C++, arrays may be one-, two-, or three-dimensional but cannot have more that three dimensions.


Recursion

9.      Write a recursive function named saywhat 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:        saywhat(1) = 3

General case:  saywhat(n) = 2 * saywhat(n-1) + 4

 

 

 

 

 

 

 

 

 

 

What would be returned by the function call saywhat(4)?  (4 points)

 

 

 

 

 

 

 

2-dimensional Arrays 

10.  Write the specified code in C++:

 

·         Define a 2-dimensional array of integers named values with 10 rows and 10 columns. (5 points)

 

 

 

 

·         Assume that the array values has been filled with integers.  Write code that will count the number of zero values in the array and display that count on the screen.  (5 points)

 

 

 

 

 

 

 

 

 

 


·         A 2-dimensional array is called diagonal if for all i ¹ j, the array entry in row i, column j is equal to zero.  The diagram below shows the locations of the zero entries.

 

Write a free function called isDiagonal 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 a 1 if the array is diagonal and a 0 otherwise. (10 points)