CSC 109 Final Examination Monday 4 May 2009 4-7 PM ===========================SUGGESTED ANSWERS============================= 1. Consider the ADT for an account in a bank. It may have a variety of sub- classes (see question two) and only has functionality for adding to the account, subtracting from the account, and retrieving the current balance. Write the declaration and definitions for this ADT, calling the class Acct. It should have sufficient functionaliy for the code below to compile and produce the indicated output. You may assume that you have the function check(bool b,char*mess) available. Acct x(20); x+=42; x-=23.2; x+=4; cout<<"The balance is $"< using namespace std; class Acct{ protected: double bal; public: Acct(double z=0); Acct & operator +=(double g); Acct & operator -=(double g); double balance()const; }; Acct:: Acct(double z):bal(z){check(z>=0,"(Acct::Acct()) negative balance");} Acct & Acct::operator +=(double g){ check(g>=0,"(Acct::+=) must have positive deposit"); bal+=g; return *this; } Acct & Acct::operator -=(double g){ check(g>=0,"(Acct::-=) must have positive deposit"); check(g<=bal,"(Acct::-=) cannot have overdrawn account"); bal-=g; return *this; } double Acct::balance()const{return bal;} ========================================================================== 2. Consider the ADT for a savings account. It has the same functionality as the ADT in question 1, plus the ability to set an interest rate and to periodically pay interest on the account. Write the declaration and definitions for the class Savings, which is a subclass of Acct. It should have sufficient functionaliy for the code below to compile and produce the indicated output. You may assume that you have the function check(bool b,char*mess) available. Savings x(100,0.10); //initial deposit of 100, interest rate 10%/year x+=100; //balance now 200 x.update(); cout<<"The balance is $"<=0,"(Savings::Savings()) cannot have negative rate"); } void Savings::setRate(double r){ rate=r; check(r>=0,"(Savings::setRate()) cannot have negative rate"); } void Savings::update(){ bal+=rate*bal; } ostream & operator<<(ostream &out,const Savings & s){ out<<"Savings("< x(100); //maximum of 100 entries List y(20); //maximum of 20 entries x.add(40).add(30).add(10); cout< class List{ private: X *data; int total,max; public: List(int n=100); List & add(const X &x); ~List(); const X & operator[](int n)const; template friend ostream & operator<<(ostream &out,const List & s); }; template List::List(int n):total(0),max(n){ check(n>0,"(List<>::List()) bad list size"); data=new X[max]; check(data!=NULL,"(List<>::List()) heap overflow"); } template List & List::add(const X &x){ check(total::add()) list overflow"); data[total]=x; total++; return *this; } template List::~List(){delete [] data;} template const X & List::operator[](int n)const{ check(n>=0 && n::[]) range error"); return data[n]; } template ostream & operator<<(ostream &out,const List & s){ out<<"List: "; for(int j=0;j(6)--[A]--(7)---+ ----(4)-------------------(6)----> | | | | +--->(9)--[B]--(4)---+---> +<---[B]<----+ | | B +-----[C]------------+ --------(7)-----------------------> | | C +<----[C]<---(8)<--+ --------(0)-----------+ | | +----(1)-----------+----> | | +----(8)--[S]--(8)-+ ========================================================================== void next(char &ch,char expect){ if(ch!=expect){ cerr<<"ERROR: '"< #include void load(int **list,int *tot); void sort(int *list,int total); void display(int *list,int total); int main(){ int *list, total; load(&list,&total); sort(list,total); display(list,total); } void load(int **list,int *tot){ int j,*temp; scanf(" %i",tot); temp=malloc(*tot*sizeof(int)); if(temp==NULL){ printf("Heap overlow\n"); exit(1); } for(j=0;j<*tot;j++){ printf("loading %i\n",j); scanf(" %i",&temp[j]); } *list=temp; } void sort(int *list,int total){ int j,done,temp; done=0; while(done==0){ done=1; for(j=0;jlist[j+1]){ temp=list[j]; list[j]=list[j+1]; list[j+1]=temp; done=0; } } } void display(int *list,int total){ int j; for(j=0;j[6]->NULL ========================================================================== struct A * addFirst(struct A *hd,int j){ struct A *temp; temp=malloc(sizeof(struct A)); if(temp==NULL){ printf("ERROR: heap failure\n"); exit(1); } temp->next=hd; temp->dat=j; return temp; } void display(struct A *hd){ while(hd!=NULL){ printf("[%i]-->",hd->dat); hd=hd->next; } printf("NULL\n"); } ========================================================================== 7. a. Assume you are using a hash table to store ints, where the int itself provides the hash number. Assume that a quadratic probe is used to handle collisions. For the table below, give the indices of the locations visited, in order, when 227 is stored in the table. (By some strange coincidence all previous items stored had values that matched the indices) 0 1 2 3 4 5 6 7 8 9 10 ---------------------------------- | 0| | | | | 5| | 7| 8| |10| ---------------------------------- b. Now write a C++ function, present(), that determines whether a given non-negative int is in the hash table. It should assume that empty locations in the table are represented by -1. The call to present() would look like int table[11],n; .... .... if(present(table,n)) cout<=0 && table[(n+rep*rep)%11]!=n){ // cout<<"Probing "<<(n+rep*rep)%11<