c109s07f

# c109s07f - CSE 109 Finaly Exam Saturday 5 May 2007...

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

CSE 109 Finaly Exam Saturday 5 May 2007 4:00-7:00 PM >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<< 1. Assume the definition of class Link below, meant to be used to construct a linked list ending in a NULL pointer. Write a function remove() that removes the first link it encounters containing a given number. If the number is not in the list, remove() does nothing. Give the declaration Link *hd; the call to remove the first link, if any, containing 10 would look like remove(hd,10). class Link{ public: int j; Link *next; }; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> void remove(Link *&h,int t){ Link * temp; if(h==NULL) return; if(h->j==t){ temp=h; h=h->next; delete temp; //avoid memory leak } else remove(h->next,t); } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2. Write a template for the class Magnitude that stores an instance of some variable or object for which "<", "==", and "<<" are defined. Instances of the class should respond to the binary operators "<", "==", and "<=". Below is some sample code. Magnitude <int> m(5), n(3); cout<<"("<<m<<"=="<<n<<")=="<<(m==n)<<endl; //OUTPUT: (5==3)==0 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> template <class C> class Mag{ public: Mag(const C&c=NULL); template <class A> friend bool operator<(const Mag<A>&a, const Mag<A> &b); template <class A> friend bool operator<=(const Mag<A>&a, const Mag<A> &b); template <class A> friend bool operator==(const Mag<A>&a, const Mag<A> &b); template <class A> friend ostream & operator<<(ostream &out,const Mag<A> &m); private: C x; }; } template <class A> Mag<A>::Mag(const A &c):x(c){} M template <class A> bool operator<(const Mag<A>&a, const Mag<A> &b){ return a.x<b.x; } template <class A> bool operator<=(const Mag<A>&a, const Mag<A> &b){ return a.x<b.x || a.x==b.x;

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

View Full Document
} template <class A> bool operator==(const Mag<A>&a, const Mag<A> &b){ return a.x==b.x; } template <class A> ostream & operator<<(ostream &out,const Mag<A> &m){ out<<m.x; return out; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 3. This questions refers to a language with the syntax given by the following syntax diagram A and associated diagrams (where
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern