c17f00f - CSc 17 Final Examination Tuesday 19 December 2000...

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

View Full Document Right Arrow Icon
CSc 17 Final Examination Tuesday 19 December 2000 >>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<< 1. Write the declaration for a class FlipFlop and then write the definition of its member functions and operators. Instances of class FlipFlop store either the value of 0 or 1. If two instances of FlipFlop are added the result is a FlipFlop which has the value 0 if both the instances being added are storing the number 0; otherwise the resulting FlipFlop has the value 1. You need only declare and implement enough functions and operators so that the following code would compile. FlipFlop a,b(0); a.set(1); cout<<"The FlipFlop has the value "<<a.get()<<endl; cout<<a<<" + "<<b<<" = "<<(a+b)<<endl; class FlipFlop{ public: FlipFlop(); FlipFlop(int n); void set(int n); int get(); FlipFlop operator+(const FlipFlop&a); friend ostream & operator<<(ostream &out, const FlipFlop & a); private: int value; }; FlipFlop::FlipFlop(){ value=0;} FlipFlop::FlipFlop(int n) {set(n);} void FlipFlop::set(int n){if(n<0 || n>1) n=0; value=n; } int FlipFlop::get(){ return value;} FlipFlop FlipFlop::operator+(const FlipFlop&a){ if(value==0 && a.value==0) return FlipFlop(0); return FlipFlop(1); } ostream & operator<<(ostream &out,const FlipFlop &a){ out<<"FlipFlop("<<a.value<<")"; return out; } 2. Write a recursive function MATCH. It is used to determine whether entries in two arrays of ints are identical. Given int a[20],b[30]; the call MATCH(a,b,2,10) returns true if and only if the entries a[2] through a[10] are identical to and in the same order as the entries b[2] through b[10]. You MUST use the following recursive algorithm. If the two sets of entries to compare have indices between n and m inclusive, the arrays match if the mth entries are the same and the rest of the two arrays match.
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
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