c109s05f - CSE 109 Final Examination Wednesday 11 May 2005...

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 Wednesday 11 May 2005 >>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<< 1. Write the prototypes and declarations for the functions called in the program below, which I intend to compile with the gcc compiler using the -xc option. The program should read two integers from the text file "final.inp" and store them, the larger first, in the text file "final.out". int main() {int a,b; getdata(&a,&b); storedata(a,b); } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include <stdio.h> void getdata(int *a, int *b); void storedata(int a, int b); void getdata(int *a, int *b) {FILE *in; in=fopen("final.inp","r"); if(in==NULL) {printf("Failure to open input file\n"); exit(1); } fscanf(in,"%d %d",a,b); } void storedata(int a, int b) {FILE *out; out=fopen("final.out","w"); if(out==NULL) {printf("Failure to open output file\n"); exit(2); } fprintf(out," %d %d ", a>b?a:b,a>b?b:a); } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2. Write a template for (the definition and code for) a class called Range that maintains the largest and smallest values it has been given, discarding the rest. It should have a constructor that takes two values of the base type to set the initial range. It should overload += to enable (the potential) addition of new values to the range, it should have methods max() and min() for access to the largest and smallest value, and it should overload << for display of the largest and smallest value, e.g., "[12, 23]". The class can assume that instances of the base type enable '<' and '>'. Here is some sample code. Range<int> x(10,20); //constructor requires min before max, min<max x+=3; //range changed to [3,20] x+=17; //range unaffected cout<<x,min()<<" "<<x.max()<<" "<<x<<endl; //"3 20 [3,20]" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include <fstream.h> # template <class X> class Range {public: Range(const X &a, const X&b); Range(); X min()const; X max()const; Range & operator+=(const X&x);
Image of page 1

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

View Full Document Right Arrow Icon
template <class Y> friend ostream & operator<<(ostream & out,const Range<Y> &r); private: X small,large; static void check(bool b, char*mess); }; } template <class X> Range<X>::Range(const X&a, const X&b):small(a),large(b) {check(a<b,"Bad initial range"); } { template <class X> Range<X>::Range(){check(false,"No range specified");} R template <class X>
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