CSE 109 Final Examination SPRING 2005

CSE 109 Final Examination SPRING 2005 - CSE 109 Final...

Info iconThis 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:
Background image of page 1

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

View Full DocumentRight Arrow Icon
Range(const X &a, const X&b); Range(); X min()const; X max()const; Range & operator+=(const X&x); template <class Y> friend ostream & operator &r); private: X small,large; static void check(bool b, char*mess); }; template <class X> {check(a<b,"Bad initial range"); } template <class X> Range<X>::Range(){check(false,"No range specified");} template <class X> X Range<X>::min()const{return small;} template <class X> X Range<X>::max()const{return large;}
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/05/2008 for the course CSE 109 taught by Professor Kay during the Spring '08 term at Lehigh University .

Page1 / 7

CSE 109 Final Examination SPRING 2005 - CSE 109 Final...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online