c109f02f - CSE 109 Final Examination Tuesday 12 December...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 109 Final Examination Tuesday 12 December 2002 **************SUGGESTED ANSWSERS*************************** 1. Write a template class for the ADT "Range." Objects of type Range store lists of objects of some type and return the maximum and minimum entry in the list. For example, the following code should produce the output indicated. Range <double> r(20); //hold a maximum of 20 entries r+=4.2; r+=12; r+=-5; cout<<r.max()<<" "<<r.min(); //displays: 12 -5 ============================================================== #include <fstream.h> #include <stdlib.h> template <class T> class Range {public: Range(int m=100); ~Range(); T max(); T min(); Range & operator+=(T a); private: T *list; int size,count; static void assert(bool b,char *mess); }; } template <class T> Range<T>::Range(int m) {assert(m>0,"(Range(int)) Need m>0"); list=new T[m]; assert(list!=NULL,"(Range(int))Heap overflow"); size=m; count=0; } template <class T> Range<T>::~Range() {delete [] list;} { template <class T> void Range<T>::assert(bool b, char *mess) {if(!b) {cerr<<"ERROR: "<<mess<<endl; exit(1); } } template <class T> Range<T>& Range<T>::operator+=(T a) {assert(count<size,"(Range +=) List overflow"); list[count]=a; count++; return *this; } template <class T> T Range<T>::max()
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
{assert(count>0,"(Range::max()) Empty list"); T big; big=list[0]; for(int j=1; j<size;j++) if(list[j]>big) big=list[j]; return big; } template <class T> T Range<T>::min() {assert(count>0,"(Range::max()) Empty list"); T small; small=list[0]; for(int j=1; j<size;j++) if(list[j]<small) small=list[j]; return small; } 2. Below is the main program from my implementation of asm2f, except that the commented line was not there. One of you discovered a bug when he wrote: 'asm2f input out input' In particular, the file 'input' got wiped out and the program crashed. I could have repaired that and similar problems by rewriting openFiles(). Instead I chose to add the function screen() indicated in the commented line. Write the protoytpe and definition of screen() so that when such problems occur the program terminates. When there is no problem, screen() does nothing and returns. int main(int ct,char **arg) {ifstream fin; ostream fout,fcode; //screen(ct,arg); openFiles(fin,fout,fcode,ct,arg); FullParser p(fin,fout,fcode; p.parse(); } ============================================================= void screen(int ct, char **arg) {if(ct!=4) exit(1); for(int j=0; j<ct; j++) for(int k=j+1; k<ct; k++) if(strcmp(arg[j],arg[k])==0) exit(2); } 3. Your goal is to write a class for parsing and evaluating expressions which satisfy the syntax diagrams and algebraic rules given in question 4.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern