A

Fall 1998 Exam 2A

1. Write a program to do the following (10 points):

a)  Clear the screen

b)  Prompt a user for a number from 2 to 10,

c)  Using a pointer, create enough memory to store that many integers,

d)  Read integers from the keyboard into the memory allocated to fill the allocated memory

e)  Drop down two lines and display the largest number entered

#include <pak.h>

void main() {

// part a

clrscr();

// part b

int iCount;

do {

cout < "How many integers do you want to enter (between 2 and 10)? ";

cin > iCount;

if (iCount < 2 || iCount > 10) {

cout < "\nWrong number of ints, please enter a number between 2 and 10";

PAK();

}

} while (iCount < 2 || iCount > 10);

// part c

int *piA = new int[iCount];

// part d

cout < "\n\nPlease enter " < iCount < " ints\n";

int iLargest = 0;

for (int i = 0; i < iCount; i++) {

cin > piA[i]; // or *(p + i)

if (piA[i] > iLargest) iLargest = piA[i];

}

// part e

cout < "\n\nLargest int entered: " < iLargest;

PAK();

} // End of Question 1


2. In the space provided, AND ONLY IN THE SPACE PROVIDED, do the following:

a)  Define an array, named iA, of type int with 5 elements, and initialize the elements to the even integers from 2 to 10 (2 points).

int iA[5] = {2, 4, 6, 8, 10};

or int iA[5]; iA[0] = 2; etc...

b)  Define a variable named pIstart of type pointer-to-int and initialize it to point to the start of the array of ints defined in a above (3 points).
int *pIstart = iA;

c)  Define a variable named pIend of type pointer-to-int and initialize it to point to the end of the array of ints defined in a above (3 points).
int *pIend = iA+4;

d)  Display the 3rd element in the array, defined in a above, to the screen using the pointer pIstart, do not change the pointer value (3 points).
cout < pIstart[2];

or cout < *(pIstart+2);

e)  Repeat number d above using the variable pIend, do not change the pointer value (5 points)..
cout < pIend[-2];

or cout < *(pIstart-2);

f)  Assume that the array defined in a above starts at memory address FFFF:AAA0, what is the address of iA[2]? (3 points).
FFFF:AAA4

g)  Assume you have already declared a class called Student. Define a variable called pStudent to point to a student object (3 points).
Student *pStudent;

h)  Define an object called StudentOne to be of type Student. Don’t worry about initializing the data members (2 points).
Student StudentOne;

i)  Make the variable defined in g above point the the StudentOne object defined in h (1 points).

pStudent = &StudentOne;
3. Write a function to create a 5-item user menu. The function should clear the screen, display 5 choices (options 'a', 'b', 'c', 'd', and 'e'), get the input from the user, check to see that the input is valid, and return the input to the calling function if it is, if the input is not valid the user should be told "Invalid input, please try again" and the menu redisplayed (20 points).

char Question3() {

char cChoice;

int iFlag;

do {

iFlag = 0;

clrscr();

cout < "\n\nMenu choices are:\n";

for (char c='a';c<'f';c++)

cout < "\t\t" < c < "\n";

cout < "\tPlease choose one: ";

cin > cChoice;

if (cChoice < 'a' || cChoice > 'e') {

iFlag = 1;

cout < "\tInvalid input, please try again!";

PAK();

}

} while (iFlag == 1);

return cChoice;

} // End of Question 3


4. Create a new data type to store a contact list. You need to store last name, first name, SSN, area code, phone number, and email address. The data members should all be declared private. Make sure you setup constructors, destructors, accessor methods, file and a method to allow the changing all of the private data in an object (this method should take arguments to fill each of the private data items). You do not need to write the functions only declare the ContactList class (10 points).

class ContactList {

private:

char* sLast;

char* sFirst;

char* sSSN;

char sAC[4];

char sPhone[10];

char* sEmail;

public:

// constructors

ContactList(); // default

ContactList(char *, char *, char *, char *, char *, char *);

//destructor

~ContactList();

//accessor methods

char *getLast(); // could also do void getLast(char []);

char *getFirst(); // ditto

char *getSSN();

char *getAreaCodePhone();

char *getEmail();

// mutator method

void changeAll(char *, char *, char *, char *, char *, char *);

void Read(istream &); // could also do void Read(); and

// open static file in the function

// for the advanced :-)

friend ostream& operator <(ostream&, ContactList &);

friend istream& operator >(istream&, ContactList &);

};


5. Write a function to read contact data from a file into an array of ContactList objects. Assume the data is formatted with a delimiter similar to the one used in homework assignment 5 (For example a record would look like: Smithe Jones|Billy Bob|123-45-6789|806|999-9999||) If you cannot figure out how to do this with private data members, then try it assuming all data in the class is public, it will not cost you many points (25 points).

// I got a little carried away, sorry.

void Question5() {

// probably should return the filled array but not required

ContactList cl[100]; // easy way waists lots of memory

int i = 0, j;

ifstream IN("Contacts.dat"); // have to open file

if (!IN) {

cout < "File not found!";

PAK();

exit(1);

}

// ------

// using extraction operator

IN > cl[i]; // lead read required in some form for all methods

while (!IN.eof()) {

i++;

IN > cl[i];

}

IN.close();

// just to show it was done

clrscr();

cout < "\nUsing extraction operator\n";

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

cout < cl[j] < "\n";

PAK();

// ------

// using Read method

i=0;

IN.open("Contacts.dat");

cl[i].Read(IN);

while (!IN.eof()) {

i++;

cl[i].Read(IN);

}

IN.close();

// just to show it was done

clrscr();

cout < "\nUsing class Read method\n";

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

cout < cl[j] < "\n";

PAK();

// ------

// using class mutator;

char sT1[99], sT2[99], sT3[99], sT4[99], sT5[99], sT6[99], sT[99];

i = 0;

IN.open("Contacts.dat");

IN.getline(sT1, 50, '|');

IN.getline(sT2, 50, '|');

IN.getline(sT3, 50, '|');

IN.getline(sT4, 50, '|');

IN.getline(sT5, 50, '|');

IN.getline(sT6, 50, '|');

IN.getline(sT,99);

while (!IN.eof()) {

cl[i].changeAll(sT1, sT2, sT3, sT4, sT5, sT6);

IN.getline(sT1, 50, '|');

IN.getline(sT2, 50, '|');

IN.getline(sT3, 50, '|');

IN.getline(sT4, 50, '|');

IN.getline(sT5, 50, '|');

IN.getline(sT6, 50, '|');

IN.getline(sT,99);

i++;

}

IN.close();

// just to show it was done

clrscr();

cout < "\nUsing class mutator\n";

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

cout < cl[j] < "\n";

PAK();

// ------

// using class constructor and array of pointers to Contacts

i = 0;

ContactList *pa[99];

IN.open("Contacts.dat");

IN.getline(sT1, 50, '|');

IN.getline(sT2, 50, '|');

IN.getline(sT3, 50, '|');

IN.getline(sT4, 50, '|');

IN.getline(sT5, 50, '|');

IN.getline(sT6, 50, '|');

IN.getline(sT,99);

while (!IN.eof()) {

pa[i] = new ContactList(sT1, sT2, sT3, sT4, sT5, sT6);

IN.getline(sT1, 50, '|');

IN.getline(sT2, 50, '|');

IN.getline(sT3, 50, '|');

IN.getline(sT4, 50, '|');

IN.getline(sT5, 50, '|');

IN.getline(sT6, 50, '|');

IN.getline(sT,99);

i++;

}

IN.close();

// just to show it was done

clrscr();

cout < "\nUsing class constructor and array of pointers to Contacts\n";

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

cout < *pa[j] < "\n";

PAK();

} // end of Question 5

6. Write a function, and only a function not a program, that will take in an array of integers from the keyboard and return the smallest value in the array to the calling function. (10 points)

int Question6() {

// very similar to Question1, way too similar in fact :-(

int *pi;

int iCount, iSmallest = 32767;

clrscr();

cout < "How many ints do you want to input? ";

cin > iCount;

pi = new int[iCount];

for(int i=0; i<iCount; i++) {

cin > pi[i];

if (pi[i] < iSmallest) iSmallest = pi[i];

}

return iSmallest;

} // end of Question6