Name  ___________________

 

CPTR246  Spring '13 (100 total points)                                                                 Exam 1

 

Classes and Objects

1.      We’re going to develop a class to help out the Relay For Life teams keep track of their donors.  Below is the definition for a class called Fundraiser.  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)

 

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

*    Fundraiser.h                                *

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

#include <iostream>

#include <string>

using namespace std;

 

class Fundraiser {

  public:

    Fundraiser(string newTeamName, string newCaptain);

         // .... see below

    double totalRaised();

         // .... see below

    void displayDonors();

         // .... see below

    bool isFull();

         // .... see below

    void newDonor(string donorName, double amount);

         // .... see below

  private:

    string teamName;        // the team’s name

    string captain;         // the name of the team’s captain

    double raised;         // total money raised so far

    int numOfDonors;        // number of donors we have so far

    string donors[50];      // array containing the names of donors

   };

 

Fundraiser:: Fundraiser(string newTeamName, string newCaptain){

// Intent:  Constructor

// Pre:   newTeamName contains the team’s name

//        newCaptain contains the name of the team’s captain

// Post:  data members initialized properly

//        NOTE:  when created, the team has not yet raised any money

//               and has no donors

 

 

 

 

 

 

 

 

 

 

 

}

double Fundraiser::totalRaised(){

// Intent: this is an accessor (“getter”) function to get the total amount

//       raised so far

// Pre: none

// Post: returns the total amount raised so far

 



}

 

void Fundraiser::displayDonors(){

// Intent: to get a list of the donors

// pre: none

// post: a list of donors is displayed on the screen

 

 

 

 

 

 

 

 

 

 

}

 

bool Fundraiser::isFull(){

// Intent: To see if there is room for more donors

// Pre: none

// Post: true if the array is full, false otherwise

 

 

 

 

 

 

 

 

}

 

void Fundraiser::newDonor(string donorName, double amount){

// Intent: To record that a new donor has committed to supporting the team;

// Pre: there is room for another donor

// Post: all appropriate instance variables (data members) are updated properly

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}


2.      Using the class definition from question 1, give C++ code (1 line) to define a Fundraiser object called myTeam where the team’s name is “Happy Coders” and you are the team captain.  (3 points)

 

 

 

 

 

 

 

3.      Using the myTeam object that you defined in question 2 and the totalRaised method from the class definition in question 1, write code that will display the total amount raised so far on the screen, right-justified in a field of width 10 with 2 decimal positions.  You may assume that the needed library has been #include’d.  (6 points)

 

 

 

 

 

 

 

 

 

 

 

 

4.      Observe that the newDoner method’s precondition is that there is room for another donor in the array.  Using the myTeam object that you defined in question 2 and the methods from the class definition in question 1, complete the code that will check to make sure that there is room for another donor, and if so, adds the donor.  Otherwise, it displays an error message.  (6 points)

 

      string name;            // name of potential donor

      double amount;          // amount to be donated

      cout << "Enter name of donor: ";

      getline(cin, name);

      cout << "Enter amount to be donated: ";

      cin >> amount;

 

 

      if (__________________________________________________________){

 

 

            __________________________________________________________ 

      }

      else {

 

            ___________________________________________________________

      {

 


Verifying input

5.      Write code that will prompt the user for an integer between 1 and 10 inclusive, gets their response, and then checks to make sure that they did indeed enter an integer between 1 and 10 inclusive.  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

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

General case:  mystery(n) = (2 * mystery(n-1)) + 5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

 

 

 

 

 

 


File I/O

7.      Make the necessary changes to the following code so that the input comes from the file (known to contain only integers) rather than from the keyboard.  The external name of the file is to come from the user at the keyboard.  (8 points)

 

#include <iostream>

 

 

using namespace std;

 

int main() {

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

     cout << "Enter integers: (ctrl-d to end)" << endl;

 

     int x;

 

    int odds = 0;

 

    int evens = 0;

 

     cin >> x;    

    

     while (!cin.eof()){

 

 

      if (x % 2 == 0)

           

            evens++;

 

      else

 

            odds++;

 

      cin >> x;

 

     }

    

     cout << “odds:  “ << odds << endl;

 

     cout << “evens: “ << evens << 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:  (5 points)

 

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

 

 

9.      Using the program below, 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            cout << "That was a " << x << endl;

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

11           cin >> x;

12     }

13     cout << endl;    

14     return 0;

15   }

 

Suppose that when the program reached line 7, the user entered “1.58 ” (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 value, if any, did the variable x receive?  (1 point)

 

 

b.      What, if anything, remains in the buffer?  (1 point)

 

 

c.       What will the function call cin.fail() return?  (1 point)


Call-by-value and Call-by-reference

10.  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){

        a = 5;

        b = 10;

        cout << a << " " << b << endl;

}

 

int main() {

        int num1 = 7;

        int num2 = 3;

        cout << num1 << " " << num2 << endl;

        someFunction (num1, num2);

        cout << num1 << " " << num2 << endl;

        return 0;

}

 

 

 

 

 

 

 

 

 

 

 

True or False

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

 

T    F     We cannot control the number of significant digits that are shown when displaying a double variable in a cout statement.

 

T    F     If a C++ program is reading input from a file, it cannot also read input from the keyboard via cin, and vice versa, if a C++ program is reading input from the keyboard via cin, it cannot also read input from a file.

 

T    F     In C++, we do not need to explicitly cast a double variable as an int in order to assign it to an int variable as we did in Java.

 

T    F     In C++, a program is made up of one or more classes, exactly one of which has a main method.


2-dimensional Arrays 

12.  Write the specified code in C++:

 

·         Define a 2-dimensional array of characters named grid with 100 rows and 200 columns. (5 points)

 

 

 

 

·         Assume that the array grid has been defined.  Write code that will fill the grid with spaces.  (5 points)

 

 

 

 

 

 

 

 

13.  Write a free function called percentBlank that takes two parameters, a two-dimensional array of characters with 100 columns and an integer for the number of rows.   It returns a double, specifically the percentage of elements in the two-dimensional array that are spaces (that is, the number of spaces in the array divided by the number of characters in the array). (10 points)