CSE 109 Final Examination Sunday 9 May 2010 ============================SUGGESTED ANSWERS============================ 1. Assume that class A has the following public methods virtual void a() { b(); } void b() { cout<<1<0) count++; while(count>0){ count--; fseek(f,count*sizeof(a),SEEK_SET); fread(&a,sizeof(a),1,f); a.amt*=1+val; fseek(f,count*sizeof(a),SEEK_SET); fwrite(&a,sizeof(a),1,f); } } =========================================================================== 4. Write a program that opens a file whose name is given on the command line, that assumes the file is a binary file of structs of type Account given in (3), that has a copy of the C code for the function update() in (3), and that adds 2% interest to each entry in the file. You may include any libraries, but you may not assume that you have available any local files. If the executable code for the program is stored in a.out, the call to the program would look like a.out aFile.bin =========================================================================== void check(int b,const char *mess,int line, const char * f); int main(int ct,char **arg){ FILE *f; check(ct==2," Usage: a.out ",__LINE__,__FILE__); f=fopen(arg[1],"r+b"); check(f!=NULL,"Failure to open input file",__LINE__,__FILE__); update(f,0.02); } void check(int b,const char *mess,int line, const char * f){ if(!b){ printf("ERROR: at line #%d in file '%s': %s\n",line,f,mess); exit(1); } } =========================================================================== 5. Create a struct Link that implements the ADT for link in a linked list, where the key also serves as the datum and is of type int. Then write the function addToList() that adds a link to the list in ascending order. Below is code that demonstrates its use. The resulting code should be able to be compiled in C, i.e., using "g++ -xc". int x[]={2,7, 9, -5, 22, 13}, j; stuct Link *head; head=NULL; for(j=0;j<6;j++) head=addToList(head,x[j]); =========================================================================== #include #include struct Link{ int k; struct Link*next; }; struct Link* addToList(struct Link*hd,int j){ struct Link*temp; if(hd==NULL || j<=hd->k){ temp=malloc(sizeof(struct Link)); if(temp==NULL){ printf("Heap overflow\n"); exit(1); } temp->k=j; temp->next=hd; return temp; } hd->next=addToList(hd->next,j); return hd; } ========================================================================= 6. Consider the ADT for the module in a coin counter that keeps track of the coins put into the machine. Write the declaration and definitions (code) for this class, so that the code below compiles and produces the given output. You may assume you have access to the usual function check(). Counter a(2,4,5,6), b(a); //start a with 2 Pennies, 4 Nickels, 5 Dimes, //and 6 quarters a.add(Counter::P,2).add(a.N,3).add(Counter::D,10).add(a.Q,1); //add 2 pennies, 3 nickels, 10 dimes, and 1 quarter cout<=0 && b>=0 && c>=0 && d>=0,"Negative value(s) not allowed", __LINE__,__FILE__); } Counter::Counter(const Counter&c){ for(int j=0;j<4; j++) count[j]=c.count[j]; } Counter&Counter::add(int type,int amt){ check(amt>=0,"Only positive values can be added",__LINE__,__FILE__); check(type>=0 && type<=3,"Bad coin type",__LINE__,__FILE__); count[type]+=amt; return *this; } ostream &operator<<(ostream &out, const Counter&c){ return out<<"Counter("< n(CountUp(2,3,4,5)); cout<<(n.child[0]==NULL)<<" "< t; // <---------------------- PROVIDE CODE t.add(CountUp(2,3,45)).add(CountUp(1,3,4,2)).add(CountUp()); //DON'T CODE cout< class Tree{ private: Node * root; public: Tree(); Tree & add(const X &x); //not provided by student template friend ostream & operator<<(ostream &out,const Tree &t); private: static void add(const X &x,Node * &rt); //not provided by student static void display(ostream &out,Node*rt); }; template Tree::Tree(){root=NULL;} /**********************************************not provided by student template Tree& Tree::add(const X&x){ add(x,root); return *this; } template void Tree::add(const X&x,Node *&rt){ if(rt==NULL) rt=new Node(x); else if (xkey) add(x,rt->child[0]); else add(x,rt->child[1]); } ****************************************************not provided by student**/ template ostream & operator<<(ostream &out,const Tree &t){ t.display(out,t.root); return out; } template void Tree::display(ostream &out,Node *rt){ if(rt!=NULL){ display(out,rt->child[0]); out<key<child[1]); } } ===========================================================================