Name  ___________________


CPTR246  Spring '03 (100 total points)                                                                   Exam 1


Classes and Objects

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



*    Sorority.h                                  *


#include <string>


#ifndef _SORORITY_H

#define _SORORITY_H


class Sorority{


    Sorority (string sororityName);

         // .... see below

    void AddASister(int studentId, string newName);

         // .... see below

    int CurrentMembership();

         // .... see below

    void DisplayMembership();

         // .... see below


    string name;            // sorority name

    int current;            // total number of sisters

    int Id[250];            // student IDs of the sisters

    string names[250];      // names of the sisters


// Note:  the arrays Id and names are parallel arrays, that is,

//        Id[i] is the student ID for names[i]





Sorority::Sorority (string sororityName){

// Intent:  Constructor

// Pre:   sorority name provided as parameter

// Post:  data members initialized properly















void Sorority::AddASister(int studentId, string newName){

// Intent: To add a member to the sorority.

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

//          newName is the sister’s name.

// Post: data members updated properly

















int Sorority::CurrentMembership (){

// Intent: to return the current number of members in the sorority

// pre: none

// post: returns the current number of members in the sorority
















void Sorority::DisplayMembership (){

// Intent: used to display the members’ information

// pre: none

// post: members’ Ids and names are displayed on the screen


















2.      Using the class definition from question 1, give C++ code to define a Sorority object, initialized to have the name "Delta Delta Delta".  (4 points)






3.      Using the class definition from question 1, give C++ code (only two statements are necessary!)  that will (1) add a sister with ID number 123456789 and name “Peggy Sue” to the sorority defined in question 2 and (2) display the information about all sorority members 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.  (10 points)


// sample program for exam # 1

#include <iostream>

using namespace std;


void mystery1(int param_1, int param_2){

        param_1 = 5;

        param_2 = param_1 + 10;

        cout << param_1 << " " << param_2 << endl;



void mystery2(int & fred, int & ethel){

        fred = 20;

        ethel = 18;

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



int main() {

        int value1 = 200;

        int value2 = 100;

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

        mystery1 (value1, value2);

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

        mystery2 (value1,value2);

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

        return 0;







Input Buffers and I/O States

5.      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()


d)    cin.ignore(80, ‘\n’);

e)    cin.peek();


_____  returns true if the user hits the key combination ctrl-d


_____  returns true if the user enters all letters


_____  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



Random Number Generation

6.      Briefly explain why it is necessary in C++ to “seed” the random number generator using the srand function.  (5 points)










Template Functions

7.      The following function prints an array of integers.  Indicate the changes that must be made to convert printArray into a template function that will print an array of integers, doubles, strings, etc.  (6 points)





void printArray(int values[], int size){

      int i;

      for (i = 0; i < size; i++)

            cout << values[i] << endl;


2-dimensional Arrays 

8.      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 replace every negative value in the array with zero.  (5 points)











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


Write a function called isUpperTriangular 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 upper triangular and a 0 otherwise. (10 points)


9.      Write a recursive function named wazzis that takes one parameter, an integer greater than or equal to 1 and returns an integer, given the following base and general cases. (10 points)


Base case:        wazzis(1) = 5

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



















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




Arrays and Functions

10.  Write a function named reverseArray that takes two parameters, a one-dimensional array of integers and an integer for the number of elements in the array.  The function is to reverse the elements in the array, i.e. the first will be last and the last will be first, etc.  The function does not return anything nor does it print anything.  (10 points)