Name  ___________________

 

CPTR246  Spring '08 (100 total points)                                                                 Exam 1

 

Classes and Objects

1.      We’re going to develop a class to help out the Book Store with their online store.  Below is the definition for a class called ShoppingCart.  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)

 

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

*    ShoppingCart.h                              *

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

#include <string>

 

class ShoppingCart{

  public:

    ShoppingCart(string ID, string password);

         // .... see below

    bool checkPassword(string enteredPassword);

         // .... see below

    bool addItem(string itemCode, double itemCost);

         // .... see below

    double getCost();

         // .... see below

    void clearCart();

         // .... see below

  private:

    string ID;              // user’s identification

    string password;       // user’s password

    double total;          // total cost of items in the shopping cart

    int numberOfItems;      // number of items in the shopping cart

    string items[30];       // array containing the product codes for up to 30

                            //    items that the user wants to buy

   };

 

ShoppingCart::ShoppingCart(string newID, string newPassword){

// Intent:  Constructor

// Pre:   newID and newPassword contain the ID and password for the person

//        for whom the shopping cart is being created

// Post:  data members initialized properly

//        NOTE:  when created, the shopping cart has no items in it

 

 

 

 

 

 

 

 

 

 

 

 

}


bool ShoppingCart::checkPassword(string enteredPassword){

// Intent: to check that the password entered by the user matches the one

//         for this person

// Pre: none

// Post: returns true if the parameter matches the password for this user

 

 

 



}

 

bool ShoppingCart::addItem(string itemCode, double itemCost){

// Intent: To add an item to the shopping cart, if the cart is not already full;

//         the cart is full if it already has 30 items in it.

// Pre: none

// Post: the items array and total cost are updated properly

//       return true if item was added to the array, false otherwise

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}

 

double ShoppingCart::getCost(){

// Intent: This is an accessor function that returns the total cost of the

//           items in the shopping cart.

// Pre: none

// Post: the total cost of the items is the shopping cart is returned

 

 

 

 

}

 

void ShoppingCart::clearCart(){

// Intent: to remove all items from the shopping cart

// pre: none

// post: the shopping cart is empty and all data members are updated accordingly

 

 

 

 

 

 

 

 

 

 

}


2.      Using the class definition from question 1, give C++ code (1 line) to define a ShoppingCart object called myCart where the user ID is your computer login and your password is “junk.”  (3 points)

 

 

 

 

 

 

 

3.      Using the myCart object that you defined in question 2 and the getCost method from the class definition in question 1, write code that will display the total cost of your shopping cart on the screen, right-justified in a field of width 10 with 2 decimal positions.  (6 points)

 

 

 

 

 

 

 

 

 

 

 

 

4.      Using the myCart object that you defined in question 2 and the checkPassword method from the class definition in question 1, write code that will prompt the user for a password, then, if the password is not correct, 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 the correct password.  (6 points)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Recursion

5.      Write a recursive function named sayWaht 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:        sayWaht(0) = 4

General case:  sayWaht(n) = (3 * sayWaht(n-1)) + 1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

What would be returned by the function call sayWaht(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){

        a = 3;

        b = 4;

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

}

 

int main() {

        int num1 = 1;

        int num2 = 2;

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

        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 myIntegers.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.      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.       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()) (on line 9) 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 “3458” (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 “1,2348” (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)

 

 

 


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 5

 

 

 

 

y =                           // a random integer between 1 and 6

 

 

 

 

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 30 rows and 20 columns. (5 points)

 

 

 

 

 

 

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

 

 

 

 

 

 

 

 

 

 


11.  A 2-dimensional array is called sparse if the entries in the array are “primarily” zero.  We will define “primarily” to mean 10% or less.

 

Write a function called isSparse that takes two parameters, a two-dimensional array of integers with 20 columns and an integer for the number of rows.   It returns a boolean, specifically true if the array is sparse (that is, 10% or fewer of the array entries are 0) and false otherwise. (10 points)