1/18/2001 down to 12/25/2000
Exam Preparation
MCX1
FINAL: Monday, January 22 from 11:50 --
1:00: Last
year's final
period 5: room 435, period 2: room
437, period 6: room 439
0. Solutions to Part I of last year's
final on the bottom of the page for 1999
final.
1.Use browser to print: 1998 A Exam:
Question 1. Then download 98a1.cpp from
usr/local/MCX1/jaye
directory. This is an excellent practice problem. 1998
A Exam: Question 1: solution
2. Keep working on 1999 final: Last year's
final. Short answer solutions to be posted Sat or Sun.
3. Periods 2 and 5: There was an error in
the remDups.cpp driver program. Go to main(), delete one of
the two calls to remDups(), and see if your solution still removes two
of the three occurrences of 5.00.
See explanation for more
info.
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
MCX1
FINAL: Monday, January 22 from 11:50 -- 1:00:
Last year's final
period 5: room 435, period 2: room
437, period 6: room 439
1. In Litvin #36 on p. 148, #1-9 on p. 159. Time yourself: See if you can do it in 19
minutes.
Log results on p. 293.
2. Bring in Litvin.
3. Use browser to copy remDups.cpp to your editor or download from /usr/local/MCX1/jaye
4. I will post answers to last year's final and other helpful exercises after Thursday, so
stay tuned.
// FILE: remDups.cpp // PURPOSE: Problem solving with vectors // FILES: apvector.h, apvector.cpp, apstring.h, apstring.cpp //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: All the files must be in directory in which you compile // 2. COMMAND LINE for g++ or gxx: g++ -Wall remDups.cpp apstring.cpp //-------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Complete remDups() as specified //---------------------------------------------------------------- #include <iomanip.h> #include "apstring.h" #include "apvector.h"
struct Rational {
Rational (int p2 = 0, int q2 = 1);
double qtnt(void) const;
int p, q;
};
Rational::Rational(int p2 = 0, int q2 = 1): p(p2), q(q2) {
if ( q < 0 ) {
p = -p; q = -q;
}
else if ( q == 0 ) {
p = 0; q = 1;
}
int n(p), d(q);
if ( n < 0 ) n = -n;
for (;;) {
int r = n % d;
if ( r == 0 ) break;
n = d; d = r;
}
p /= d; q /= d;
}
double Rational::qtnt(void) const {
if ( q != 0 ) return static_cast<double>(p)/q;
else return 0.0;
}
bool operator==(const Rational &left, const Rational &right) {
return (left.p == right.p && left.q == right.q);
}
bool operator<(const Rational &left, const Rational &right) {
if ( left == right ) return false;
else return ( left.qtnt() < right.qtnt() );
}
bool operator>(const Rational &left, const Rational &right) {
if ( left == right ) return false;
else return ( left.qtnt() > right.qtnt() );
}
ostream &operator<<(ostream &ostr, const Rational &r) {
static bool printInit(false);
if ( !printInit ) {
ostr << setiosflags(ios::fixed | ios::showpoint) << setprecision(2);
printInit = true;
}
if ( r.q >= 0 )
return ostr << r.p << "/" << r.q << " = " << r.qtnt();
else
return ostr << r.p << "/(" << r.q << ") = " << r.qtnt();
}
template<class T>
void remDups(apvector<T> &v) {
// precond: v is in ascending order
// postcond: all duplicate elements in v are removed
}
template<class T>
void print(const apvector<T> &v, char eol) {
const int SIZE = v.length();
for (int k = 0; k < SIZE; k++)
cout << setw(4) << k << setw(4) << ""
<< v[k] << eol;
}
template<class T>
void sort(apvector<T> &v) {
const int SIZE = v.length();
for (int current = 0; current < SIZE - 1; current++) {
int minPos = current;
for (int pos = current+1; pos < SIZE; pos++) {
if ( v[pos] < v[minPos] )
minPos = pos;
}
T temp = v[current];
v[current] = v[minPos];
v[minPos] = temp;
}
}
void loadRationalVec(apvector<Rational> &);
int main() {
cout << "\n\n\n\n\n\n\n\n\t\t"
<< "Remove Duplicates Function\n\n";
apvector<Rational> V; loadRationalVec(V); sort(V); cout << "Before remDups():\n"; print(V, '\n'); cout << "\nAfter remDups():\n"; remDups(V); print(V, '\n');
cout << endl; return 0; }
void loadRationalVec(apvector<Rational> &v) {
Rational A[] = { Rational(35, 7), Rational(81, 5), Rational(131, 20),
Rational(150, 7), Rational(-11, 2), Rational(81, 5),
Rational(-11, 2), Rational(81, 5), Rational(150, 7),
Rational(56, 3) };
int SIZE = sizeof(A)/sizeof(Rational);
v.resize(SIZE);
for (int k = 0; k < SIZE; k++) {
v[k] = A[k];
}
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
MCX1
FINAL: Monday, January 22 from 11:50 -- 1:00:
Last year's final
1. From /usr/local/MCX1/jaye directory,
download these 4 files: Lists.h, Lists.cpp, test09.cpp,
makeTest09
1a. If you use gxx, change command in makeTest09 from g++ etc.. to gxx
etc...
2. Complete bSearch() and findPositions() in test09.cpp
3. Follow instructions on building program with this command: make -f makeTest09
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
MCX1
FINAL: Monday, January 22 from 11:50 -- 1:00:
Last year's final
In streams2.cpp, complete readChars() as specfied in its postcondition
(frequency distribution chart).
Use the get() istream function to accomplish the task:
ch = inFile.get();
Consider using inFile.eof() to determine end of file.
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
MCX1
FINAL: Monday, January 22 from 11:50 -- 1:00:
Last year's final
In streams2.cpp, complete assignment due Thursday, 1/11. That doesn't include the readChars() function.
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
MCX1
FINAL: Monday, January 22 from 11:50 -- 1:00:
Last year's final
1.Use browser to copy streams2.cpp to
your editor or download from /usr/local/MCX1/jaye and
follow the given instructions.
// FILE: streams2.cpp // PURPOSE: Use istream and ostream member functions to create, use, // and modify files // COMPILE/LINK (for g++ and gxx): g++ (or gxx) -Wall streams2.cpp apstring.cpp // FILES CREATED: intDouble.dat, Pair.dat //===================================================================== // PROGRAMMING INSTRUCTIONS: // 1. Complete readFormatted() as specified below // 2. a. Overload << for the Pair class // b. Complete writeStruct() as specified below // 3. Validate writeStruct() by running readformatted() on file // created in 2b (Pair.dat) // 4. (in future: readChars() to print frequency distribution chart) //====================================================================== #include <fstream.h> #include <iomanip.h> #include <stdlib.h> #include "apvector.h" #include "apstring.h"
struct Pair {
Pair(int= 0, double = 0.0);
int iVal;
double dVal;
};
Pair::Pair(int i, double d):
iVal(i), dVal(d)
{}
ostream &operator<<(ostream &ostr, const Pair pair) {
}
void Continue(void); void skipLines(int); void outFileState(ofstream &, const apstring &); void inFileState(ifstream &, const apstring &); void writeFormatted(ofstream &); void writeStruct(istream &, ofstream &);
void readFormatted(ifstream &inFile) {
// precond: inFile consisting of int double pairs is open for input
//postcond: the mean of the ints and largest int reported
// the mean of the doubles and smallest double reported
int intMax(-10000); //*** assume this is adequate
double doubleMin(1e9); //*** assume this is adequate
// If you want to print the input on the fly you can use these stmts:
//cout << setiosflags(ios::showpoint | ios::fixed) << setprecision(3);
//cout << " # int double" << endl;
cout << "\n\t*** readFormatted() not installed.***" << endl;
}
void writeStruct(istream &inFile, ofstream &outFile) {
// precond: inFile and outFile are open for reading & writing, respectively
//postcond: using overloaded << (for Pair), inFile's data is used for outFile
cout << "\n\t*** writeStruct() not installed.***" << endl;
}
void readChars(ifstream &inFile) {
// precond: inFile is open for input
//postcond: prints frequency distribution of each character digits, '.',
// spaces, and other characters
cout << "\n\t*** readChars() not installed.***" << endl;
}
int main() {
cout << "\n\n\n\n\n\n\n\t\t"
<< "Testing istream and ostream member functions with files\n\n";
const apstring fmtFileName("intDouble.dat"), pairFileName("Pair.dat");
ofstream outFormatted;
//==========================================
// Writes file as an int and double per line
//==========================================
outFormatted.open(fmtFileName.c_str());
cout << fmtFileName << " opened for output:\n";
outFileState(outFormatted, fmtFileName);
writeFormatted(outFormatted);
outFormatted.close();
cout << fmtFileName << " closed:\n";
outFileState(outFormatted, fmtFileName);
Continue(); skipLines(24);
//==========================================
// 1. inFormatted() reads file as int double per
// line and finds statistical measures
//==========================================
ifstream inFormatted;
inFormatted.open(fmtFileName.c_str());
cout << fmtFileName << " opened for input as int double:\n";
readFormatted(inFormatted);
inFormatted.close();
cout << fmtFileName << " closed:\n";
inFileState(inFormatted, fmtFileName);
Continue(); skipLines(24);
//==========================================
// 2. writeStruct() reads in int double file
// and uses overloaded << to write to new file
//==========================================
inFormatted.open(fmtFileName.c_str());
outFormatted.open(pairFileName.c_str());
cout << pairFileName << " opened for output as Pair objects:\n";
writeStruct(inFormatted, outFormatted);
inFormatted.close(); outFormatted.close();
Continue(); skipLines(24);
//===============================================
// 3. inFormatted() validates writeStruct() since
// the stats for new file should be equal
//===============================================
inFormatted.open(pairFileName.c_str());
cout << pairFileName << " (Pair objects) opened for input as int double:\n";
readFormatted(inFormatted);
inFormatted.close();
Continue(); skipLines(24);
//===============================================
// 4. readChars() reads in file by character and
// generates a frequency distribution chart
//===============================================
inFormatted.open(fmtFileName.c_str());
cout << fmtFileName << " opened for input as chars:\n";
readChars(inFormatted);
inFormatted.close();
return 0; }
void outFileState(ofstream &outFile, const apstring &fname) {
cout << "***** STATE OF " << fname << ":\teof() = "
<< outFile.eof() << "\tfail() = " << outFile.fail() << endl;
}
void inFileState(ifstream &inFile, const apstring &fname) {
cout << "***** STATE OF " << fname << ":\teof() = "
<< inFile.eof() << "\tfail() = " << inFile.fail() << endl;
}
void Continue(void) {
cout << "Press <ENTER> to continue: ";
cin.ignore(5, '\n');
}
void skipLines(int lines) {
for (int k = 1; k <= lines; k++) {
cout << endl;
}
}
void writeFormatted(ofstream &outFile) {
Pair A[] = { Pair(17, 3.75), Pair(16, 4.75), Pair(15, 5.75),
Pair(14, 6.75), Pair(13, 7.75), Pair(12, 8.75),
Pair(11, 9.75), Pair(29, 4.21), Pair(24, -5.21),
Pair(120, 6.66), Pair(19, 0.02) };
const int SIZE = sizeof(A)/sizeof(Pair);
for (int k = 0; k < SIZE; k++) {
outFile << A[k].iVal << ' ' << A[k].dVal << endl;
cout << A[k].iVal << ' ' << A[k].dVal << endl;
}
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
MCX1
FINAL: Monday, January 22 from 11:50 -- 1:00:
Last year's final
1.Use browser to copy streams1.cpp to
your editor or download from /usr/local/MCX1/jaye and
follow the given instructions.
// FILE: streams1.cpp // PURPOSE: To demonstrate istream and ostream member functions // COMPILE/LINK (for g++ and gxx): g++ streams1.cpp apstring.cpp //===================================================================== // INSTRUCTIONS: // 1. Run getPut() to test your system's cout buffering and its ^Z or ^D // 2. Comment out call to getPut() and run getInt1() // 3. Comment out call to getInt1() and complete getInt2() in which // you accomplish same task without cin action in while condition // and by using sentinel value like -1 instead of CTRL-D or CTRL-Z //====================================================================== #include <iomanip.h> #include <stdlib.h> #include "apstring.h"
void getInt2(void) {
// Obtain average using trailer data (instead of CTRL-Z or CTRL-D)
// and WITHOUT cin operation in loop condition
cout << "\n\n\n\n\t\tFinding an average: "
<< "(cin operation NOT in condition)\n\n";
int score, sum, count;
cout << "\t*** getInt2() not installed ***" << endl;
}
void printState(void); void getInt(void); void getPut1(void);
int main() {
getPut1();
//getInt1();
//getInt2();
return 0; }
void printState(void) {
cout << "cin.eof() = " << cin.eof()
<< "\tcin.fail() = " << cin.fail();
cout << "\ncout.eof() = " << cout.eof()
<< "\tcout.fail() = " << cout.fail() << endl;
cout << "EOF = " << EOF << endl;
}
void getInt1(void) {
cout << "\n\n\n\n\t\tFinding an average: "
<< "(cin operation in condition)\n\n";
int score, sum, count;
sum = count = 0;
cout << "Enter score (CTRL-Z or CTRL-D to quit): " << flush;
while ( cin >> score ) {
sum += score;
count++;
cout << "Enter score (CTRL-Z or CTRL-D to quit): " << flush;
}
if ( count > 0 )
cout << "\n\nMean of " << count << " scores = "
<< static_cast<double>(sum)/count << endl;
else
cout << "\n\nNo basis for average." << endl;
printState();
cout << "Comment out the call to getInt1() "
<< "and implement getInt2()" << endl;
// exit(0);
}
void getPut1() {
char ch;
cout << "\n\n\n\n\n\t\t" << "Testing get() and put()\n\n";
printState();
cout << "\nEnter a sentence followed by <RETURN>: " << flush;
while ( ( ch = cin.get() ) != '\n' )
cout.put( ch );
cout << "\nEnter a sentence followed by <RETURN> (flush in loop): "
<< flush;
while ( (ch = cin.get()) != '\n' ) {
cout.put( ch ); cout.flush();
}
cout << "\nEnter a sentence followed by ^Z or ^D: " << flush;
while ( ( ch = cin.get() ) != EOF ) {
cout.put( ch );
}
cout << endl << endl;
printState();
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
Test Tuesday 01/09
MCX1
FINAL: Monday, January 22 from 11:50 -- 1:00:
Last year's final
1. Prepare to implement binary search for List container class by doing the
following:
1. Add this prototype to class definition in List.h: int bSearch(const U
&) const;
2. Use browser to copy testbSearch()
below to your current version of ListMain.cpp use
it as test driver for
member function bSearch(). (Don't forget to call testbSearch(Lst);
in
main() function!!)
3. Implement bSearch() in List.cpp
2. Add bool operator==(const List &right) const;
to List.h, implement it in List.cpp, and
test it by adding appropriate code to ListMain.cpp.
Suggestion: testAssign() is an ideal function
for this since Lists A and B are first not equal and then equal.
3. Read sections 6.2 (RandGen class), 6.4.2 (Position class) and 6.4.3 (Neighborhood
class)
in Litvin. You are responsible for understanding (not memorizing) the
contents of header files
randgen.h, position.h, and nbrhood.h
void testbSearch(List<Student> &L) {
apstring sName;
char Continue;
do {
cout << "\n\n\n\n\n\n\n\n";
cout << "\t3. Testing List's binary search function:\n\n";
cout << "\tList<Student>'s elements:\n";
lprint(L);
cout << "Enter name to search for: ";
getline(cin, sName);
int index = L.bSearch(Student(sName));
if ( index >= 0 ) {
cout << sName << " found at index position " << index << endl;
cout << "L[" << index << "] = " << L[index] << endl;
}
else {
cout << sName << " not found...\n";
}
cout << "\nAnother binary search? (Y/N): ";
cin.get(Continue); cin.ignore(10, '\n');
} while ( Continue != 'N' && Continue != 'n' );
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
Test Tuesday 01/09
Use browser to copy List.h, List.cpp, and
ListMain.cpp to your editor or download from
/usr/local/MCX1/jaye and implement operator=() and sort() member functions for List
container class in List.cpp.
Hint: If you can't get selection sort algorithm to work, check vector3.cpp or vector3s.cpp from earlier this term.
// FILE: List.h
// PURPOSE: Header file forList class template
// OTHER FILES: apvector.h, apvector.cpp, apstring.h, apstring.cpp
//---------------------------------------------------------------------
#ifndef LIST_H
#define LIST_H
#include <iostream.h> #include <iomanip.h> #include "apstring.h" #include "apvector.h"
template <class U>
class List {
public:
List(void);
List(int size, const U &fillVal);
List(const List &);
int getCount(void) const;
int getMax(void) const;
void print(const char *) const;
int find(const U &) const;
U &operator[](int index);
void sort(void);
bool Insert(const U &item);
bool Delete(const U &item);
List &operator=(const List &right);
private:
int maxSize;
apvector<U> vec;
int count;
};
#include "List.cpp" #endif // FILE: List.cpp // PURPOSE: Implement List class template // OTHER FILES: apvector.h, apvector.cpp, apstring.h, apstring.cpp //--------------------------------------------------------------------- #include <iostream.h> #include <iomanip.h> #include "apstring.h" #include "apvector.h" #include "List.h"
template <class U>
List<U>::List(void):
maxSize(5), vec(maxSize, U()), count(0)
{}
template <class U>
List<U>::List(int size, const U &fillVal):
maxSize(size), vec(maxSize, fillVal), count(0)
{}
template <class U>
List<U>::List(const List<U> &L):
maxSize(L.maxSize), vec(L.vec), count(L.count)
{}
template <class U>
int List<U>::getMax(void) const {
return maxSize;
}
template <class T>
int List<T>::getCount(void) const {
return count;
}
template <class T>
void List<T>::print(const char *vName2) const {
apstring vName(vName2);
for (int k = 0; k < maxSize; k++) {
if ( k > 0 ) cout << ", ";
cout << vName + "[" << k << "]=" << vec[k];
}
}
template <class U>
U &List<U>::operator[](int index) {
return vec[index];
}
template <class U>
int List<U>::find(const U &item) const {
for (int k = 0; k < maxSize; k++)
if ( vec[k] == item ) return k;
return -1;
}
template <class U>
List<U> &List<U>::operator=(const List<U> &r) {
cout << "\t*** operator=() not installed\n";
}
template <class U>
void List<U>::sort(void) {
cout << "\t*** sort() not installed\n";
}
// FILE: ListMain.cpp // PURPOSE: Driver for List container class: // USER-DEFINED CLASS FILES: List.h, List.cpp // OTHER FILES: apvector.h, apvector.cpp, apstring.h, apstring.cpp //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: All the files must be in directory in which you compile // 2. COMMAND LINE: g++ (or gxx) ListMain.cpp apstring.cpp //-------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Complete operator=() and sort() member functions in List.cpp // and run this program to validate your solutions //---------------------------------------------------------------- #include <iostream.h> #include <iomanip.h> #include "apstring.h" #include "apvector.h" #include "List.h"
class Student {
public:
Student(const apstring &name2= "", int crdts = 0, int hrs = 0);
apstring getName(void) const;
double getGPA(void) const;
void print(void) const;
void setGradeData(int newCrdts, int newHrs);
private:
apstring name;
int credits;
int hours;
};
Student::Student(const apstring &name2, int crdts, int hrs):
name(name2), credits(crdts), hours(hrs)
{}
apstring Student::getName(void) const {
return name;
}
double Student::getGPA(void) const {
if ( hours > 0 ) return double(credits)/hours;
else return -1.0;
}
void Student::print(void) const {
static int calls(0);
if ( calls == 0 )
cout << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
cout << name << ": Credits = " << credits << ", Hours = "
<< hours << ", GPA = " << setw(4) << getGPA();
calls++;
}
bool operator>(const Student &left, const Student &right) {
return (left.getName() > right.getName());
}
bool operator<(const Student &left, const Student &right) {
return ( right > left );
}
bool operator==(const Student &left, const Student &right) {
return ( left.getName() == right.getName() );
}
ostream &operator<<(ostream &ostr, const Student &student) {
student.print(); return ostr;
}
template<class T>
void lprint(List<T> &L) {
const int SIZE = L.getMax();
for (int k = 0; k < SIZE; k++)
cout << '\t' << k << ". " << L[k] << endl;
cout << endl;
}
void testAssign(void); //*** tests List<T>'s operator=() void loadStudent(List<Student> &); void testSort(List<Student> &);
int main() {
cout << "\n\n\n\n\t\tTesting List container class\n\n";
testAssign(); cout << "\n\nPress <ENTER> to continue: "; cin.ignore(10, '\n'); cout << "\n\n\n\n\n";
List<Student> Lst(8, Student()); loadStudent(Lst); testSort(Lst);
cout << endl; return 0; }
///////////////////////////////////////////
// DRIVERS
///////////////////////////////////////////
void testAssign(void) {
cout << "1. Testing List's operator=() function:\n\n";
List<double>A(4, -3.75), B, C(5, 9.8);
cout << "\tA: "; A.print("A"); cout << endl;
cout << "\tB: "; B.print("B"); cout << endl;
cout << "\tAfter the assignment B = A:\n";
B = A;
cout << "\tB: "; B.print("B"); cout << endl << endl;
cout << "\tC: "; C.print("C"); cout << endl;
cout << "\tAfter the assignment A = B = C:\n";
A = B = C;
cout << "\tA: "; A.print("A"); cout << endl;
cout << "\tB: "; B.print("B"); cout << endl;
}
void loadStudent(List<Student> &L) {
Student A[] = {
Student("Zach", 80, 25), Student("Pat", 100, 42),
Student("Kim", 0, 0), Student("Bev", 250, 84), Student("Mike", 90, 32),
Student("Ali", 70, 20), Student("Nan", 45, 18), Student("Bo", 20, 5) };
int SIZE = sizeof(A)/sizeof(Student);
if ( SIZE > L.getMax() ) SIZE = L.getMax();
for (int k = 0; k < SIZE && k < L.getMax(); k++)
L[k] = A[k];
}
void testSort(List<Student> &L) {
cout << "2. Testing List's sort() function:\n\n";
cout << "Before sort:\n";
lprint(L);
cout << "After sort:\n";
L.sort();
lprint(L);
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. Read Hubbard pp. 300-306 (sections 13.1 through 13.4).
2. Use browser to paste container1.cpp below to your editor
or download from /usr/local/MCX1/jaye,
enter your solutions and validate them by running the program.
// FILE: container1.cpp // PURPOSE: Implement class template // OTHER FILES: apvector.h, apvector.cpp, apstring.h, apstring.cpp //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: All the files must be in directory in which you compile // 2. COMMAND LINE: g++ (or gxx) container1.cpp apstring.cpp //-------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // 1. Complete the constructors, getMax(), and getCount() and run // driver1 to validate your solutions // 2. Complete operator[](), &operator[](), and find() and run driver2 //---------------------------------------------------------------- #include <iostream.h> #include <iomanip.h> #include "apstring.h" #include "apvector.h"
template <class U>
class List {
public:
List(void);
List(int size, const U &fillVal);
List(const List &);
int getCount(void) const;
int getMax(void) const;
void print(const char *) const;
int find(const U &) const;
U operator[](int index) const;
U &operator[](int index);
void sort(void);
bool Insert(const U &item);
bool Delete(const U &item);
//List &operator=(List &right);
private:
int maxSize;
int count;
apvector<U> vec;
};
template <class U>
List<U>::List(void) {
cout << "\t***Default constructor not installed ****\n";
}
template <class U>
List<U>::List(int size, const U &fillVal) {
cout << "\t***Explicit-value constructor not installed ****\n";
}
template <class U>
List<U>::List(const List<U> &L) {
cout << "\t***Copy constructor not installed ****\n";
}
template <class U>
int List<U>::getMax(void) const {
cout << "\t***getMax() not installed ****\n";
return 2;
}
template <class T>
int List<T>::getCount(void) const {
cout << "\t***getCount() not installed ****\n";
return -3;
}
template <class T>
void List<T>::print(const char *vName2) const {
apstring vName(vName2);
for (int k = 0; k < maxSize; k++) {
if ( k > 0 ) cout << ", ";
cout << vName + "[" << k << "]=" << vec[k];
}
}
template <class U>
U List<U>::operator[](int index) const {
cout << "\t***operator[]() not installed ****\n";
return U();
}
template <class U>
U &List<U>::operator[](int index) {
cout << "\t***&operator[]() not installed ****\n";
return vec[0];
}
template <class U>
int List<U>::find(const U &item) const {
cout << "\t***find() not installed ****\n";
return -1;
}
void driver1(void); void driver2(void);
int main() {
cout << "\n\n\n\n\t\tTesting List container class\n\n";
driver1(); //*** tests constructors, getMax(), getCount(), and print() cout << "\n\nPress <ENTER> to continue: "; cin.ignore(10, '\n'); cout << "\n\n\n\n\n"; driver2(); //*** tests operator[](), &operator[](), and find()
cout << endl << endl; return 0; }
///////////////////////////////////////////
// DRIVERS
///////////////////////////////////////////
template <class T>
void driver1A(const List<T> &L, const char *msg) {
cout << '\t' << msg;
cout << "L.getMax() = " << L.getMax() << " L.getCount = "
<< L.getCount() << endl;
}
void driver1(void) {
//------------------------------------------------------------
// Tests the 3 constructors, getMax(), getCount(), and print()
//------------------------------------------------------------
List<double> A, B(6, 1.1);
List<apstring> C(3, "John Doe");
cout << "1. Testing constructors, getMax(), getCount(), and print():\n\n"; driver1A(A, "After List<double> A: "); driver1A(B, "After List<double> B(6, 1.1):"); driver1A(C, "After List<apstring> C(3, \"John Doe\"):"); List<double> D(B); driver1A(D, "After List<double> D(B):"); cout << endl;
cout << "\tA: "; A.print("A"); cout << endl;
cout << "\tB: "; B.print("B"); cout << endl;
cout << "\tD: "; D.print("D"); cout << endl;
cout << "\tC: "; C.print("C"); cout << endl;
}
template <class T>
void driver2A(List<T> &L, const char *vName2) {
const apstring vName(vName2);
cout << "\tindexing " << vName << " with operator[]() and &operator[]:\n";
const int MAX = L.getMax();
cout << "\t";
for (int k = 0; k < MAX; k++) {
if ( k > 0 ) cout << ", ";
cout << vName + "[" << k << "] = " << L[k];
L[k] += k;
}
cout << endl << "\t";
L.print(vName2); cout << endl;
}
void driver2(void) {
//------------------------------------------------------------
// Tests tests operator[](), &operator[](), and find()
//------------------------------------------------------------
List<double> A(4, 12.3);
List<char> B(5, 'A');
cout << "\n2. Testing operator[](), &operator[](), and find():\n\n";
driver2A(A, "A"); cout << endl;
for (int k = 0; k < A.getMax(); k++) {
cout << "\tA.find(A[" << k << "]) = " << A.find(A[k])
<< "\tA.find(" << k + 5 << ") = " << A.find(k + 5) << endl;
}
A[0] = A[1] = A[2] = A[3];
cout << "\n\tAfter A[0] = A[1] = A[2] = A[3]:\n\t";
A.print("A"); cout << endl << endl;
driver2A(B, "B");
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. Take 10-minute practice test in Litvin's Exam A (p.129) problems 31-35 Set a
timer for 10 minutes and STOP
at that time. Record your result on p. 293.
2. Download 1995 A Exam, Question 2 and
try to complete in 30 minutes or less.
3. Use browser to paste 95a2.cpp below to your editor or
download from /usr/local/MCX1/jaye
and validate your solutions therein.
// FILE: 95a2.cpp // PURPOSE: Test implementation of solutions to 1995 Exam A, Question 2 // OTHER FILES: apvector.h, apvector.cpp, apstring.h apstring.cpp, // apmatrix.h, and apmatrix.cpp //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: All the files must be in directory in which you compile // 2. COMMAND LINE: g++ (or gxx) 95a2.cpp apstring.cpp //-------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Print out 1995 A Exam Question 2 (at web site) and implement your // solutions as specified //---------------------------------------------------------------- #include <iostream> #include <iomanip> #include "apstring.h" #include "apvector.h" #include "apmatrix.h"
void Swap(int & a, int & b);
//*********************************
// part a *
//*********************************
void ReverseArray(apvector<int> & a) {
// postcondition: elements of a are reversed
}//*** ReverseArray
//*********************************
// part b *
//*********************************
void ReverseVertical(apmatrix<int> & m, int col) {
// precondition: 0 <= col < m.numcols()
}//*** ReverseVertical
//*********************************
// part c *
//*********************************
void ReverseMatrix(apmatrix<int> & m) {
// precondition: m.numrows() == m.numcols()
}//*** ReverseMatrix
void partADriver(void); void partBDriver(void); void partCDriver(void); void print(apvector<int>, apstring); void print(apmatrix<int>, apstring);
int main() {
cout << "\n\n\n\n\n\n\t\t\t1995 Exam A: Question 2\n\n";
partADriver(); partBDriver(); cout << "Press <ENTER> to continue: "; cin.ignore(10, '\n'); cout << "\n\n\n\n\n\n\n"; partCDriver();
return 0; }//*** main
void partADriver(void) {
int A[5] = {61, 34, 18, 99, 73};
const int size = sizeof(A)/sizeof(int);
apvector<int> V(size);
for (int k = 0; k < size; k++)
V[k] = A[k];
print(V, "Before ReverseArray: ");
ReverseArray(V);
print(V, "After ReverseArray: ");
cout << endl;
}//*** partADriver
void partBDriver(void) {
const int n = 4;
int M[n][n] = { {61, 34, 18, 99},
{11, 22, 44, 33},
{55, 66, 77, 88},
{25, 45, 35, 55} };
apmatrix<int> apM(n, n);
for (int r = 0; r < n; r++)
for (int c = 0; c < n; c++)
apM[r][c] = M[r][c];
print(apM, "\nBefore ReverseVertical:\n"); ReverseVertical(apM, 2); print(apM, "After ReverseVertical:\n");
cout << endl << endl; }//*** partBDriver
void partCDriver(void) {
const int n = 4;
int M[n][n] = { {61, 34, 18, 99},
{11, 22, 44, 33},
{55, 66, 77, 88},
{25, 45, 35, 55} };
apmatrix<int> apM(n, n);
for (int r = 0; r < n; r++)
for (int c = 0; c < n; c++)
apM[r][c] = M[r][c];
print(apM, "Before ReverseMatrix:\n");
ReverseMatrix(apM);
print(apM, "After ReverseMatrix:\n");
cout << endl << endl;
}//*** partCDriver
void print(apvector<int> v, apstring msg) {
cout << msg;
for (int k = 0; k < v.length(); k++)
cout << setw(4) << v[k];
cout << endl;
}//*** print
void print(apmatrix<int> m, apstring msg) {
cout << endl << msg;
for (int k = 0; k < m.numrows(); k++) {
print(m[k], "");
}
cout << endl;
}//*** print
void Swap(int & a, int & b) {
int tmp = a;
a = b;
b = tmp;
}//*** Swap
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. Execute both Part I case study programs to validate system and prepare for
reading:
a. g++ (or gxx) aquamain.cpp aquafish.cpp randgen.cpp
b. g++ (or gxx) sixflips.cpp aquafish.cpp randgen.cpp
2. Skim through case study (pp. 7-17) and check all answers to all questions in Part 1 Answers.
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. Read Experimenting
with the program, which summarizes pp. 19-28 in Case Study.
2. Compile and link case study program:
a. For g++ you need
Makefile and for gxx
you need GNUmakefile. Both are available by
going to
Marine
Biology case study page at this web site.
b. Enter make (just
make)
c. Execute by entering ./fishsim
3. Read pp. 19-28 in Case Study and check all your solutions in solutions to pp. 19-28
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. Read Learning how the
program stores fish, which summarizes pp. 29-36 in Case Study.
2. Read pp. 29-36 in Case Study and check all your solutions in solutions to pp. 29-36
1a. Read Learning
how fish interact with the environment, which summarizes pp. 36-42 in Case Study.
1b. Read pp. 36-42 in Case Study and check all your solutions in solutions to pp. 36-42
2a. Read Understanding the rest of
the program, which summarizes pp. 43-48 in Case Study.
2b. Read pp. 43-48 in Case Study and check all your solutions in solutions to pp. 43-48
Due Tues 01/02: Extra Credit Project
1. To be eligible, your average must be less than 80 and
you cannot have been marked unprepared
for homework more than twice.
2. Your project must run on g++ (do not use Borland or Microsoft features that will make
it impossible
to compile or link or run correctly in the school's Room 307 lab).
3. You must hand in your project on Tuesday, Jan 2
in the form of:
1. hard copy
2. on disk (so I can test it)
4. Here's the project specifications:
Using the class definition given below, write a program that uses the TicTac class to play
a game of tic-tac-toe
against the computer.
class TicTac {
public:
TicTac(void);
// dimensions board to 3 x 3 and sets it to all spaces
void computerMoves(void); // computer always uses capital
'O'
void playerMoves(int r, int c); // player always uses capital 'X'. r
and c come from cin.
void display(void);
// displays the board after every move and announces final result.
bool gameOver(void);
// returns true when
game has been won or ends in a tie
private:
bool computerWon(void); // returns true when
computer has won
bool playerWon(void);
// returns true when player has won
apmatrix<char> board;
// 3 x 3 matrix of char
int numMoves;
// total number of moves that computer and player have made
};
In adddition to the requirements commented in to the definition your
program must meet these:
1. The computer's opponent is given the option of going first or second.
2. If the computer goes first, the worst result it should have is a tie.
3. If the computer goes second, the worst result it should have is a tie.
4. After every move, the board is printed and the state of the board is checked to
determine
whether or not the game is over.
5. When a game is over, the final results are announced.
6. Do NOT change the question or requirements or you will not receive credit for
the project.