12/1/00 down to 11/13/00
Due 12/01 Friday
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. Read sections 11.1 and 11.4 (overloading arithmetic operators).
2. Use browser to copy classes3.cpp below to your editor or download it from /usr/local/MCX1/jaye.
Then complete the program as specified in the PROGRAMMING
INSTRUCTIONS.
// FILE: classes3.cpp // PURPOSE: Implement overloaded operators and function templates // that use them // OTHER FILES: apvector.h, apvector.cpp, apstring.h apstring.cpp //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: apvector.h, apvector.cpp apstring.h, apstring.cpp must // be in folder or directory in which you compile // 2. COMMAND LINE: // g++ (or gxx) classes3.cpp apstring.cpp //--------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // 1. Complete the defintions of the relational operators started below // and run program to validate your solutions // 2. Complete BOTH max() function templates started below and run // program to validate your solutions //--------------------------------------------------------------------- #include <iostream.h> #include <iomanip.h> #include "apvector.h" #include "apstring.h"
class Customer {
public:
Customer(void);
Customer(apstring n2, double bal);
Customer (const Customer &customer);
void deposit(double amt);
void withdraw(double amt);
bool isGreater(const Customer &) const;
apstring getName(void) const;
double getBalance(void)const;
private:
apstring name;
double balance;
};
////////////////////////////////////////////////////////////////////// // member functions defined and not defined //////////////////////////////////////////////////////////////////////
Customer::Customer(void):
name("John Doe"), balance(0.0) {
}
Customer::Customer(apstring name2, double balance2):
name(name2), balance(balance2) {
}
Customer::Customer(const Customer &customer):
name(customer.name), balance(customer.balance){
}
apstring Customer::getName(void) const {
return name;
}
double Customer::getBalance(void)const {
return balance;
}
bool Customer::isGreater(const Customer &right) const {
return ( name > right.name );
}
void Customer::deposit(double amt) {
balance += amt;
}
void Customer::withdraw(double amt) {
if ( balance - amt < 0 )
balance -= 20.00; //** penalty for attempted over-draft
else
balance -= amt;
}
////////////////////////////////////////////////////////////////////// // free (non-member) functions defined and not defined //////////////////////////////////////////////////////////////////////
bool operator == (const Customer &left, const Customer &right) {
return ( false );
}
bool operator == (const apstring &left, const Customer &right) {
return ( false );
}
bool operator != (const Customer &left, const Customer &right) {
return ( false );
}
bool operator != (const apstring &left, const Customer &right) {
return ( false );
}
bool operator>(const Customer &left, const Customer &right) {
return ( false );
}
bool operator>(const apstring &left, const Customer &right) {
return ( false );
}
bool operator >= (const Customer &left, const Customer &right) {
return ( false );
}
bool operator >= (const apstring &left, const Customer &right) {
return ( false );
}
bool operator<(const Customer &left, const Customer &right) {
return ( false );
}
bool operator<(const apstring &left, const Customer &right) {
return ( false );
}
bool operator<=(const Customer &left, const Customer &right) {
return !( right > left );
}
bool operator<=(const apstring &left, const Customer &right) {
return ( false );
}
template <typename Type>
Type max(Type A, Type B) {
return Type();
}
template <typename Type>
Type max(const apvector<Type> &V) {
const int SIZE = V.length();
return Type();
}
void relationalOps(void); void templateTest(void); void printVec(const apvector<Customer> &, const apstring &);
int main(void) {
relationalOps();
cout << "\nPress <Enter>: "; cin.ignore(10, '\n');
templateTest();
cout << endl << endl;
return 0;
}//*** main
void relationalOps(void) {
apstring title("Testing free overloaded relational operators");
cout << "\n\n\n\n\n\t\t" << title << endl << endl;
Customer X("Bob", 123.12), Y("Eve", 765.76), Z("Pat", 987.01);
apstring str("Bob");
cout << "X.name = " << X.getName() << "\tY.name = " << Y.getName()
<< "\tapstring str = \"" << str << "\"" << endl << endl;
cout << "(X == Y) = " << (X == Y) << "\t(X == X) = " << (X == X)
<< "\t(str == X) = " << (str == X) << endl;
cout << "(X != Y) = " << (X != Y) << "\t(X != X) = " << (X != X)
<< "\t(str != X) = " << (str != X) << endl << endl;
cout << "(X > Y) = " << (X > Y) << "\t(X > X) = " << (X > X)
<< "\t(str > X) = " << (str > X) << endl;
cout << "(X >= Y) = " << (X >= Y) << "\t(X >= X) = " << (X >= X)
<< "\t(str >= X) = " << (str >= X) << endl << endl;
cout << "(X < Y) = " << (X < Y) << "\t(X < X) = " << (X < X)
<< "\t(str < X) = " << (str < X) << endl;
cout << "(X <= Y) = " << (X <= Y) << "\t(X <= X) = " << (X <= X)
<< "\t(str <= X) = " << (str <= X) << endl;
}
void templateTest(void) {
apstring title("Testing templated and overloaded max()");
cout << "\n\n\t\t" << title << endl << endl;
Customer A[] = {Customer("Flo", 24), Customer("Zoe", 13), Customer("Moe", 39),
Customer("Bo", 15), Customer("Jo", 21) };
//-----------------------------------------------------------
Customer maxCust = max(A[0], A[3]);
cout << "max(25, 35) = " << max(25, 35) << endl;
cout << "max( Customer(\"Flo\", 24), Customer(\"Bo\", 15)) = "
<< "Customer(\"" << maxCust.getName() << "\", "
<< maxCust.getBalance() << ")" << endl;
//------------------------------------------------------------
const int SIZE = sizeof(A)/sizeof(Customer);
apvector<Customer> v(SIZE);
for (int k = 0; k < SIZE; k++)
v[k] = A[k];
printVec(v, "Testing templated max() on this apvector<Customer> object:");
cout << "max(v).getName = " << max(v).getName() << endl;
//-----------------------------------------------------------
int B[] = { 17, 15, 18, 19, 11 };
apvector<int> iVec(sizeof(B)/sizeof(int));
for (int k = 0; k < iVec.length(); k++)
iVec[k] = B[k];
cout << "\nTesting templated max() in this apvector<int> object:\n";
for (int k = 0; k < iVec.length(); k++)
cout << setw(2) << k << setw(8) << iVec[k] << endl;
cout << "max(iVec) = " << max(iVec) << endl;
}
void printVec(const apvector<Customer> &A, const apstring &msg) {
cout << "\n" << msg << endl;
const int ColSIZE = 12;
const int SIZE = A.length();
cout << setiosflags(ios::fixed | ios::right) << setprecision(2)
for (int k = 0; k < SIZE; k++)
cout << setw(3) << k << " " << A[k].getName()
<< setw(ColSIZE - A[k].getName().length()) << ""
<< setw(7) << A[k].getBalance() << endl;
}
//*** end of program
.
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. Read sections 13.1 thru 13.2 (function templates).
2. In vector3.cpp from 11/22
Tues:
A. For the g++ (NOT gxx) compiler:
1. Change the prototype: void
selectSort(apvector<class T> &);
to: template <class T>
void selectSort(apvector<class T> &);
2. Change the selectSort() function to a function template.
3. Run the program to validate your solution.
B. For the gxx) compiler:
1. Delete the prototype void
selectSort(apvector<class T> &);
2. Copy selectSort() and paste it BEFORE the
main().
3.Change the selectSort() function to a function template.
4. Run the program to validate your solution.
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
In classes2.cpp:
1. complete addCustomer() and ...
2. Define and implement a boolean Customer member function isGreater() as specified below.
EXAMPLE: given Customer X("Bob",
13.17), Y("Eve", 450.01);
1. X.isGreater(Y) == false
(because X.name > Y.name == false) but
2. Y.isGreater(X) == true
(because Y.name > X.name == true)
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. In classes2.cpp from the
previous assignment, add a new menu selection:
$1000+ balances
7
Then implement a list1000() function which lists the
names and balances of all customers with
balances greater than or equal $1000.
2. Read section 10.7 (copy constructor)
3. Add a copy constructor declaration to the Customer class definition on
the line below
the constructor declaration Customer(apstring
n2, double bal); Then implement the
copy constructor and run the program to validate your solution.
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. In the classes1.cpp program in
part 1 of the previous assignment (due 11/22 Weds),
complete
programming problem #3, the sortByBalance() function.
2. Use browser to copy classes2.cpp below to
your editor or download it from /usr/local/MCX1/jaye.
Then complete Parts #1 and #2 (on different days, if possible). Part #3
won't be due until Tues or Weds.
Remember that Customer object data members name
and balance are now private.
3. Read Review Questions 10.1 thru 10.6 on p. 248 and be sure you can
answer these questions
based on your reading and what we've learned in class. (Answers to the Review
Questions are
on p. 250)
// FILE: classes2.cpp // PURPOSE: Use (be a client of) a bank Customer class // OTHER FILES: apvector.h, apvector.cpp, apstring.h apstring.cpp //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: apvector.h, apvector.cpp apstring.h, apstring.cpp must // be in folder or directory in which you compile // 2. COMMAND LINE: // g++ (or gxx) classes2.cpp apstring.cpp //--------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // 1. Complete bSeach() and withdrawDriver() as specified below // 2. Complete averageBalance() and deleteCustomer() as specified below // 3. Complete addCustomer() as specified below. (Not due Monday) //--------------------------------------------------------------------- #include <iostream.h> #include <iomanip.h> #include "apvector.h" #include "apstring.h"
class Customer {
public:
Customer(void);
Customer(apstring n2, double bal);
void deposit(double amt);
void withdraw(double amt);
apstring getName(void) const;
double getBalance(void)const;
private:
apstring name;
double balance;
};
int bSearch(const apvector<Customer> &v, const apstring &key) {
// precondition: v's elements are in ascending order
//postcondition: when Customer's name = key, its index is returned
// otherwise -1 is returned
int lo(0), hi(v.length()-1);
return -1;
}
void withdrawDriver(apvector<Customer> &V) {
// precondition: bSearch() function works!!!
//postcondition: this is the counterpart to depositDriver(). Therefore
// use depositDriver() as a model to complete this function
cout << "*** withdrawDriver() not installed yet\n";
}
void addCustomer(apvector<Customer> &V) {
// HINT: don't use bSearch()--use a linear search here!!
//postcondition: specified Customer object is added by shifting
// all successors (if any) back and V is resized accordingly
// EXAMPLE adding L: A D G K M Q X ----> A D G K L M Q X
// 0 1 2 3 4 5 6 0 1 2 3 4 5 6 7
cout << "*** addCustomer() not installed yet\n";
}
void deleteCustomer(apvector<Customer> &V) {
// precondtion: bSearch() works!!
//postcondition: if found, specified Customer object is removed by
// shifting all successors (if any) forward and V is resized accordingly
// EXAMPLE deleting L: A D G K L M Q X ----> A D G K M Q X
// 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6
cout << "*** deleteCustomer() not installed yet\n";
}
void averageBalance(const apvector<Customer> &V) {
//postcondtion: reports average balance of all customers,
// the largest and the smallest balance
cout << "*** averageBalance() not installed yet\n";
}
void loadVec(apvector<Customer> &); void printVec(const apvector<Customer> &, const apstring &); void menu(char &); void bSearchDriver(const apvector<Customer> &); void depositDriver(apvector<Customer> &);
int main(void) {
apvector<Customer> cVec;
char ch;
loadVec(cVec);
do {
printVec(cVec, "");
menu(ch);
switch( ch ) {
case '1': bSearchDriver(cVec); break;
case '2': depositDriver(cVec); break;
case '3': withdrawDriver(cVec); break;
case '4': addCustomer(cVec); break;
case '5': deleteCustomer(cVec); break;
case '6': averageBalance(cVec); break;
case '0': return 0;
default: break;
} // switch
cout << "Press <ENTER> to continue..."; cin.ignore(80, '\n');
} while (ch != '0');
cout << endl << endl; return 0; }//*** main
//////////////////////////////////////////////////////////////////////
// free (non-member) functions that have been defined
//////////////////////////////////////////////////////////////////////
void loadVec(apvector<Customer> &A) {
Customer list[] =
{Customer("Boden", 2920.20), Customer("Ferrara", 666.77), Customer("Gell", 7000),
Customer("Green",757.99), Customer("Jaye", 1267.13), /*Customer("Latham", 110.6),*/
Customer("Liang", 1199.88), Customer("Platek", 15.15), Customer("Simion", 1919.19),
Customer("Winokur", 23.23), Customer("Zaman", 260.26) };
const int SIZE = sizeof(list)/sizeof(Customer);
A.resize(SIZE);
for (int k = 0; k < SIZE; k++) {
A[k] = list[k];
}
}
void printVec(const apvector<Customer> &A, const apstring &msg) {
cout << "\n\n\n\n\n" << msg;
const int ColSIZE = 12;
const int SIZE = A.length();
for (int k = 0; k < SIZE; k++)
cout << setw(30) << k << " " << A[k].getName()
<< setw(ColSIZE - A[k].getName().length()) << ""
<< setiosflags(ios::fixed | ios::right) << setprecision(2)
<< setw(7) << A[k].getBalance() << endl;
cout << endl;
}
void menu(char &choice) {
const apstring TAB("\t\t\t");
cout << TAB << " Bank Customer List Operations\n";
cout << TAB << "----------------------------------\n";
cout << TAB << "Binary search on customer name 1\n";
cout << TAB << "Make a deposit 2\n";
cout << TAB << "Make a withdrawal 3\n";
cout << TAB << "Add a new customer 4\n";
cout << TAB << "Delete a customer 5\n";
cout << TAB << "Average balance 6\n";
cout << TAB << "Quit program 0\n";
cout << TAB << "----------------------------------\n";
cout << TAB << " SELECTION==> ";
for (;;) {
cin.get(choice); cin.ignore(10, '\n');
if ( choice < '0' || choice > '6' )
cout << TAB << "*** Invalid choice. Try again: ";
else
break;
}
} //*** menu
void bSearchDriver(const apvector<Customer> &V) {
apstring name;
cout << "Enter customer name to be searched: ";
getline(cin, name);
int index = bSearch(V, name);
if ( index < 0 )
cout << name << " not found.\n";
else {
cout << name << " found at index position " << index
<< ", Balance = " << V[index].getBalance() << endl;
}
}
void depositDriver(apvector<Customer> &V) {
apstring name;
cout << "Enter customer name: ";
getline(cin, name);
int index = bSearch(V, name);
if ( index < 0 ) {
cout << name << " not found.\n";
}
else {
cout << "Enter amount to deposit: ";
double deposit;
cin >> deposit; cin.ignore(10, '\n');
V[index].deposit(deposit);
cout << "New balance is: " << V[index].getBalance() << endl;
}
}
//////////////////////////////////////////////////////////////////////
// member functions that have been defined
//////////////////////////////////////////////////////////////////////
void Customer::deposit(double amt) {
balance += amt;
}
void Customer::withdraw(double amt) {
if ( balance - amt < 0 )
balance -= 20.00; //** penalty for attempted over-draft
else
balance -= amt;
}
apstring Customer::getName(void) const {
return name;
}
double Customer::getBalance(void)const {
return balance;
}
Customer::Customer(apstring name2, double balance2):
name(name2), balance(balance2) {
}
Customer::Customer(void):
name("John Doe"), balance(double()) {
}
//*** end of program
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. a. Use your browser to copy the program below to your editor and complete the
functions as specified OR
b. On your school account copy the file with this command: cp
/usr/local/MCX1/jaye/classes1.cpp ~
2. Review sections 10.1-10.4, read section 10.5
// FILE: classes1.cpp // PURPOSE: Develop bank customer class // OTHER FILES: apvector.h, apvector.cpp, apstring.h apstring.cpp //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: apvector.h, apvector.cpp apstring.h, apstring.cpp must // be in folder or directory in which you compile // 2. COMMAND LINE: // g++ (or gxx) classes1.cpp apstring.cpp //--------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // 1. Use selection sort algorithm to complete sortByName() as specified // 2. Complete addOneThousand() as specified below // 3. Due Mon: complete sortByBalance() as specified and write code // in main() to drive and test it //--------------------------------------------------------------------- #include <iostream.h> #include <iomanip.h> #include "apvector.h" #include "apstring.h"
class Customer {
public:
apstring name;
double balance;
};
//*********************************
// Part 1
//*********************************
void sortByName(apvector<Customer> &v) {
//postcondition: Elements of v are in ascending order by name
const int SIZE = v.length();
}
//*********************************
// Part 2
//*********************************
void addOneThousand(apvector<Customer> &v) {
//postcondition: 1000 is added to balance of every customer whose
//name contains two or more LOWERCASE vowels
//Note: Remember that the length of the name data member of v[k] is v[k].name.length()
const int SIZE = v.length();
}
//************************************
// Part 3: This is due Mon, NOT Weds
//************************************
void sortByBalance(apvector<Customer> &v) {
//postcondition: Elements of v are in DESCENDING order by balance
}
void loadVec(apvector<Customer> &); void printVec(const apvector<Customer> &, const apstring &);
int main(void) {
apstring title("Building a Bank Customer Class");
cout << "\n\n\n\n\n\n\n\n";
cout << setw(38 + title.length()/2) << title.c_str() << endl << endl;
apvector<Customer> cv; loadVec(cv);
printVec(cv, "Printing vector of Customer before sortByName():\n"); sortByName(cv); printVec(cv, "Printing vector of Customer AFTER sortByName():\n"); cout << "Press <ENTER> to continue: "; cin.ignore(10, '\n'); addOneThousand(cv); printVec(cv, "Printing vector of Customer AFTER addOneThousand():\n");
cout << endl << endl; return 0; }//*** main
void loadVec(apvector<Customer> &A) {
apstring nameList[] = {"Schim", "Gell", "Ferr", "Boden", "Platek", "Winokur",
"Jaye", "Zaman", "Green", "Avig", "Lath" };
const int SIZE = sizeof(nameList)/sizeof(apstring);
double balanceList[] = { 190.19, 70.7, 60.66, 20.22, 150.99, 230.46,
100, 260.85, 65.77, 10.66, 120.13 };
A.resize(SIZE);
for (int k = 0; k < SIZE; k++) {
A[k].name = nameList[k];
A[k].balance = balanceList[k];
}
}
void printVec(const apvector<Customer> &A, const apstring &msg) {
cout << msg;
const int ColSIZE = 10;
const int SIZE = A.length();
for (int k = 0; k < SIZE; k++)
cout << setw(3) << k << " " << A[k].name
<< setw(ColSIZE - A[k].name.length()) << ""
<< setiosflags(ios::fixed | ios::right) << setprecision(2)
<< setw(7) << A[k].balance << endl;
cout << endl;
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
1. a. Use your browser to copy the program below to your editor and complete the
functions as specified OR
b. On your school account copy the file with this command: cp
/usr/local/MCX1/jaye/vector3.cpp ~
// FILE: vector3.cpp // PURPOSE: Implement selection sort algorithm on apvector object // OTHER FILES: apvector.h, apvector.cpp, apstring.h apstring.cpp //--------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: apvector.h, apvector.cpp, apstring.h, apstring.cpp,randgen.h, // randgen.cpp in folder or directory in which you compile // 2. COMMAND LINE: // g++ (or gxx) vector3.cpp apstring.cpp randgen.cpp //--------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Complete selectSort() specified below //---------------------------------------------------------------- #include <iomanip.h> #include "apstring.h" #include "apvector.h" #include "randgen.h"
void loadVec(apvector<apstring> &); void printVec(const apvector<apstring> &); bool isSorted(const apvector<apstring> &); void selectSort(apvector<apstring> &);
int main() {
const apstring title("Testing Selection Sort Function");
const int TESTS(5);
apvector<apstring> v;
int goodSorts(0);
for (int test = 1; test <= TESTS; ++test) {
cout << "\n\n\n\n\n\n\n\n\n" << setw(39 + title.length()/2)
<< title.c_str() << endl << endl;
cout << "Test " << setw(2) << test << " of " << TESTS << endl << endl;
loadVec(v);
cout << "Unsorted vector:\n";
printVec(v);
cout << "Vector after selection sort function:\n";
selectSort(v);
printVec(v);
if ( isSorted(v) ) {
goodSorts++;
cout << "selectSort() succeeeded!!\n";
}
else
cout << "selectSort() failed\n";
cout << "\nPress <ENTER> to continue: ";
cin.ignore(10, '\n');
}
cout << "\nSorts:Successful sorts = " << TESTS
<< ":" << goodSorts << endl;
cout << endl;
return 0;
}
//------------------------------------------------
// Selection sort
//------------------------------------------------
void selectSort(apvector<apstring> &V) {
//postcondition: V's elements are in ascending order
const int SIZE = V.length();
}
///////////////////////////////////////////////////////////////// // Already defined functions /////////////////////////////////////////////////////////////////
void loadVec(apvector<apstring> &V) {
apstring A[] = {"Avig", "Boden", "Ferr", "Gell", "Green", "Jaye",
"Lath", "Platek", "Schim", "Zaman" };
const int SIZE = sizeof(A)/sizeof(apstring);
RandGen r(97);
if ( V.length() != SIZE )
V.resize(SIZE);
for (int k = 0; k < SIZE; k++) {
V[k] = A[k];
}
for (int k = 0; k < 3*SIZE; k++) {
int p;
do {
p = r.RandInt(0, SIZE - 1);
} while ( p == k );
apstring temp = V[k % SIZE];
V[k % SIZE] = V[p];
V[p] = temp;
}
}
void printVec(const apvector<apstring> &V) {
const int SIZE = V.length();
for (int k = 0; k < SIZE; k++)
cout << setw(2) << k << ". " << V[k] << endl;;
cout << endl;
}
bool isSorted(const apvector<apstring> &V) {
const int SIZE = V.length();
for (int k = 0; k < SIZE - 1; k++) {
if ( V[k] > V[k+1] )
return false;
}
return true;
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
0. Due Monday, November 20:
Purchase Be Prepared for the AP Computer Science Exam,
Maria Litvin. Book
information and online purchasing outlets
1. Bring AP Exam Quick Reference handout (2-sided description of the AP classes
distributed in class
about two weeks ago). If you lost it, print it out at AP Exam Quick
Reference
2. Read pp. 232-238: sections 10.1, 10.2, 10.3, 10.4 (on C++ classes)
3. a. Print out: 1993 AP A Exam Question 1
b. Use browser to copy code below to your editor or download 93a1.cpp
from /usr/local/MCX1/jaye
// FILE: 93a1.cpp // PURPOSE: Test implementation of solutions to 1993 Exam A, Question 1 // OTHER FILES: apvector.h, apvector.cpp, apstring.h apstring.cpp //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: apvector.h, apvector.cpp apstring.h, apstring.cpp must // be in folder or directory in which you compile // 2. COMMAND LINE: // g++ (or gxx) 93a1.cpp apstring.cpp //-------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Print out 1993 A Exam Question 1 and implement your solutions // to part a, part b, and part c as specified //---------------------------------------------------------------- #include <iostream.h> #include <iomanip.h> #include <limits.h> #include "apvector.h" #include "apstring.h"
void loadVectors(apvector<long> &, apvector<long> &, apvector<long> &); void print(const apvector<long> &, const apstring &);
//***********************************
// part a *
//***********************************
int PosOfMax(const apvector<long> &A) {
const int SIZE = A.length();
return -77;
}//*** PosOfMax
//***********************************
// part b *
//***********************************
int PosOf2nd(apvector<long> &A) {
return -77;
}//*** PosOf2nd
//***********************************
// part c *
//***********************************
void ZeroBetween(apvector<long> &A) {
}//*** ZeroBetween
void main(void) {
apvector<long> B, C, D;
loadVectors(B, C, D);
cout << "\n\n\n\n\n\n\n\n";
cout << setw(46) << "1993 AP EXAM A: Question 1 Solutions" << endl;
cout << "\nPosOfMax: " << PosOfMax(B) << ", PosOf2nd: "
<< PosOf2nd(B) << endl;
print(B, "Before ZeroBetween: ");
ZeroBetween(B);
print(B, "After ZeroBetween: ");
cout << "\nPosOfMax: " << PosOfMax(C) << ", PosOf2nd: "
<< PosOf2nd(C) << endl;
print(C, "Before ZeroBetween: ");
ZeroBetween(C);
print(C, "After ZeroBetween: ");
cout << "\nPosOfMax: " << PosOfMax(D) << ", PosOf2nd: "
<< PosOf2nd(D) << endl;
print(D, "Before ZeroBetween: ");
ZeroBetween(D);
print(D, "After ZeroBetween: ");
cout << endl << endl; }//*** main
void loadVectors(apvector<long> &A, apvector<long> &B, apvector<long> &C) {
long arrayA[]= {8, 91, 18, 27, 4, -6, 85, 57};
long arrayB[]= {23, 15, 8, 19, 6, 12, 21, 30};
long arrayC[] = {34, 15, 3, 99, 88, 27, 16, -1};
const int SIZE = sizeof(arrayA)/sizeof(long);
A.resize(SIZE); B.resize(SIZE); C.resize(SIZE);
for (int k = 0; k < SIZE; k++) {
A[k] = arrayA[k]; B[k] = arrayB[k]; C[k] = arrayC[k];
}
}
void print(const apvector<long> &A, const apstring &msg) {
cout << msg;
const int SIZE = A.length();
for (int k = 0; k < SIZE; k++)
cout << setw(4) << A[k];
cout << endl;
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
0. Due Monday, November 20:
Purchase Be Prepared for the AP Computer Science Exam,
Maria Litvin. Book
information and online purchasing outlets
1. Bring AP Exam Quick Reference handout (2-sided description of the AP classes
distributed in class
about two weeks ago). If you lost it, print it out at AP Exam Quick
Reference
2. a. Use your browser to copy the program below to your editor and complete the functions
as specified OR
b. On your school account copy the file with this command: cp
/usr/local/MCX1/jaye/vector2.cpp ~
// FILE: vector2.cpp // PURPOSE: Implement search algorithms on apvector objects // OTHER FILES: apvector.h, apvector.cpp, apstring.h apstring.cpp //--------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: apvector.h, apvector.cpp, apstring.h apstring.cpp must // be in folder or directory in which you compile // 2. COMMAND LINE: // g++ (or gxx) vector2.cpp apstring.cpp //--------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Complete linearSearch() and binarySeach() as specified below //---------------------------------------------------------------- #include <iomanip.h> #include "apstring.h" #include "apvector.h"
void loadVec(apvector<apstring> &); void printVec(const apvector<apstring> &); int linearSearch(const apvector<apstring> &, const apstring &); int binarySearch(const apvector<apstring> &, const apstring &);
int main() {
const apstring title("Testing Search Algorithms");
apvector<apstring> V;
loadVec(V);
for (;;) {
cout << "\n\n\n\n\n\n\n\n\n" << setw(39 + title.length()/2)
<< title.c_str() << endl << endl;
printVec(V);
cout << "Enter name to search for (or <ENTER> to quit): ";
apstring name;
getline(cin, name);
if ( name.length() == 0 ) break;
int index = linearSearch(V, name);
cout << "\nResult of linear search: ";
if ( index >= 0 )
cout << name << " found at index position " << index << ".\n";
else
cout << name << " not found.\n";
index = binarySearch(V, name);
cout << "\nResult of binary search: ";
if ( index >= 0 )
cout << name << " found at index position " << index << ".\n";
else
cout << name << " not found.\n";
cout << "\nPress <ENTER> to continue: ";
cin.ignore(10, '\n');
}
cout << endl;
return 0;
}
//------------------------------------------------
// Linear search algorithm
//------------------------------------------------
int linearSearch(const apvector<apstring> &V, const apstring &item) {
// precondition: vector V is sorted and contains no duplicates
//postcondition: If item is in V, its index is returned, else -1 is returned
return -77;
}
//------------------------------------------------
// Binary search
//------------------------------------------------
int binarySearch(const apvector<apstring> &v, const apstring &item) {
// precondition: vector V is sorted and contains no duplicates
//postcondition: If item is in V, its index is returned, else -1 is returned
return -77;
}
///////////////////////////////////////////////////////////////// // Already defined functions /////////////////////////////////////////////////////////////////
void loadVec(apvector<apstring> &V) {
apstring A[] = {"Avig", "Boden", "Ferr", "Gell", "Green", "Jaye",
"Lath", "Platek", "Schim", "Zaman" };
const int SIZE = sizeof(A)/sizeof(apstring);
V.resize(SIZE);
for (int k = 0; k < SIZE; k++)
V[k] = A[k];
}
void printVec(const apvector<apstring> &v) {
const int SIZE = v.length();
for (int k = 0; k < SIZE; k++)
cout << setw(2) << k << ". " << v[k] << endl;;
cout << endl;
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
0. Due Monday, November 20:
Purchase Be Prepared for the AP Computer Science Exam,
Maria Litvin. Book
information and online purchasing outlets
1. Bring AP Exam Quick Reference handout (2-sided description of the AP classes
distributed in class
about two weeks ago). If you lost it, print it out at AP Exam Quick
Reference
2. On your school account copy the file with this command: cp
/usr/local/MCX1/jaye/vector1.cpp ~
NOTE: Make sure that your apstring.h
file does NOT #include <apstring.cpp>. This directive,
which is
found at the end of some versions of the
apstring.h file, will cause a linking error. Just delete this #include.
.
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
0. Due Monday, November 20:
Purchase Be Prepared for the AP Computer Science Exam,
Maria Litvin. Book
information and online purchasing outlets
1. Bring AP Exam Quick Reference handout (2-sided description of the AP classes
distributed in class
about two weeks ago). If you lost it, print it out at AP Exam Quick
Reference
2. Read about array basics: 6.1, 6.2, 6.4 and const qualifier: 10.9
3. Use your browser to copy the program below to your editor and complete the functions as
specified.
// FILE: array.cpp // PURPOSE: One-dimensional array operations //========================================================================== #include <iostream.h> #include <iomanip.h> #include "apstring.h" //=============================================================== // INSTRUCTIONS: // 1. Complete findLargest() as specified below // 2. Complete invalidIndex() as specified below //================================================================ void findLargest(void); void invalidIndex(void);
int main() {
findLargest();
invalidIndex();
cout << endl;
return 0;
}
void findLargest(void) {
// precondition: SIZE > 0, list[] contains no duplicate elements
//postcondition: variable index contains position of largest element
cout << "\n\n\n\n\n\t\t\tTesting findLargest():\n"
<< "function should determine that index position of largest element = 4\n"
<< "and that the value of the largest element is 100.1\n\n";
const int SIZE(10);
double list[SIZE] = { 3.5, -4.7, 100, 7, 100.1, 23, -57, -0.09, 99, -77 };
int index(0);
//------------------------------------------------------------------------
// YOUR CODE:
//------------------------------------------------------------------------
cout << "\nindex position = " << index
<< "\tlargest value = " << list[index] << endl << endl;
}
void invalidIndex(void) {
// 1. run the program to see the effects of out of bounds indices
// 2. uncomment the last statement to test the effect of a wild index
const int SIZE(2);
int inFront(500);
int table[SIZE] = {1000, 2000};
int inBack(2500);
cout << "\n\n\n\t\tTesting effects of invalid index in invalidIndex()\n\n"
<< "Initial values: inFront: " << inFront << ", table[0]: " << table[0]
<< ", table[1] = " << table[1] << ", inBack: " << inBack << endl;
table[2] = -77;
cout << "\nAfter statement table[2] = -77;:\n"
<< "inFront: " << inFront << ", table[0]: " << table[0]
<< ", table[1] = " << table[1] << ", inBack: " << inBack << endl;
table[-1] = 666;
cout << "\nAfter statement table[-1] = 666;:\n"
<< "inFront: " << inFront << ", table[0]: " << table[0]
<< ", table[1] = " << table[1] << ", inBack: " << inBack << endl;
//---------------------------------------------------------------------
// After observing the effects of table[2] = -77 and table[-1] = 666,
// uncomment the statement below to see if it crashes the program (ABEND)
// table[100000] = 1234;
//---------------------------------------------------------------------
}
Homework and tests must be kept in chronological order in a folder
or a loose leaf binder
0. Due Monday, November 20:
Purchase Be Prepared for the AP Computer Science Exam,
Maria Litvin. Book
information and online purchasing outlets
1. Read 5.10 (passing by reference), 5.11 (passing by constant reference), 5.13
(scope)
2. Answer review questions (p. 112) 5.1, 5.2, 5.3, 5.7, 5.8, 5.9 (Bring answers to class)
3. Use mouse and browser to copy program below to your editor and complete it as
specified.
4. Can you design a toRadix() for radix > 10 with no decision with respect to the
successive remainders?
// FILE: toInt.cpp
// PURPOSE: Use function to convert string representing # in scale of notation
// specified by given radix to an integer value
//==========================================================================
#include <iostream.h>
#include <iomanip.h>
#include "apstring.h"
//===============================================================
// INSTRUCTIONS:
// 1. Complete toInt() as specified below
// 2. Run the program to test your solution
//================================================================
int toInt(apstring, int);
apstring toRadix(int, int);
void toIntDriver(void);
int main() {
toIntDriver();
cout << endl << endl;
return 0;
}
int toInt(apstring number, int r) {
// precondition: number represents number in radix r, 2 <= r <= 10
//postcondition: returns numerical value that parameter number represents
int sum(-77);
return sum;
}
apstring toRadix(int n, int r) {
// precondition: n >= 0, 2 <= r <= 10
//postcondtion: Returns st which is a representation of n
// in r's scale of notation
apstring st;
do {
st = char( '0' + n % r ) + st;
n /= r;
} while ( n != 0 );
return st;
}
void toIntDriver(void) {
apstring title("Using toInt() to convert from radix scales from 2 to 10 ");
cout << "\n\n\n\n\n\n" << setw(38 + title.length()/2) << title.c_str() << endl << endl;
apstring numbers[] = { "111111", "1000000", "101110", "7777", "10000", "52073",
"9999", "10000", "4907"};
int bases[] = { 2, 2, 2, 8, 8, 8, 10, 10, 10};
const int VALUES = sizeof(numbers)/sizeof(apstring);
for (int k = 0; k < VALUES; ++k) {
int toIntResult = toInt(numbers[k], bases[k]);
cout << "toInt(" + numbers[k] + ", " << bases[k] << ") = " << toIntResult;
if ( toRadix(toIntResult, bases[k]) == numbers[k] )
cout << " OK\n";
else
cout << " ***** ERROR\n";
}
cout << endl;
}