CSc 17 Final Examination Friday 15 May 1998 8-11AM Page 1 >>>>>>>>>>>>>>>>>>>>ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 1. (25 pts) One should never retrieve the contents of a variable before one has stored a value in the variable. An ADT SafeVar could prevent this error. Consider the ADT SafeChar, with the following properties: (1) A character can be stored in a SafeChar; (2) The value of SafeChar can be retrieved only after (1) has occurred otherwise an error occurs. Write a class SafeChar which implements this ADT, i.e., write both the declaration and the definitions (implementation). class SafeChar {public: SafeChar(); char get(); void put(char ch); private: char kh; bool stored; }; SafeChar::SafeChar() {stored=false;} void SafeChar::put(char ch) {kh=ch; stored=true;} char SafeChar::get() {if(!stored) {cout<<"Error: tried to access undefined value.\n" <<"Press to quit program...."; cin.get(); } else return kh; } 2. (10 pts) Expand the above specification of the ADT so that it allows input and output. (1) The same as above. (2) Read from a file into SafeChar. (3) Retrieve a char from SafeChar only after either (1) or (2) has occurred, else an error. (4) Write the contents of SafeChar to a file only after either (1) or (2) has occurred, else an error. 3. (20 pts) Write a recursive function REVERSE() such for the declarations int first, last, nums[20]; the call REVERSE(first,last,nums) reverses the order of the entries in nums between (and including) locations first and last. For example, if nums=[5,6,2,3,4,7,2,6,....], after the call REVERSE(2,5,nums) nums=[5,6,7,4,3,2,2,6,...]. YOU MUST USE THE FOLLOWING ALGORITHM: TO REVERSE THE NUMBERS BETWEEN FIRST AND LAST SWAP THE NUMBERS IN FIRST AND LAST AND THEN REVERSE ALL THE NUMBERS IN BETWEEN. void reverse(int first, int last, int ray[]) { if(first aFunc(T a, T b, T c); template bFunc(T a, U b); 6. (10 pts) Given the following data structure, write the pre- and post-conditions for the function X(): class ONE { public: int key; ONE *child[]; } void X(ONE *rt,int val) //Pre-Conditions: rt points to an allocated node or NULL and so do all of rt's descendants. //Post-Conditions: rt is unchanged, all values in the tree larger than val have been printed. {if (rt!=NULL) {X(rt->child[0],val); if(r->key>val) cout<key; X(rt->child[1],val); } } 7. (25 pts) Write a function CountLines() so that when the following fragment of code is executed the number of lines of text in the file FINAL.TXT is displayed on the screen: ifstream in("FINAL.TXT"); cout << "The file FINAL.TXT has "<key<0) head->key=-head->key; head=head->next; } } 11. (20 pts) Given the declarations int last,y[5]; write an int-valued function "numerate()" such that when "last" has some value between 0 and 4 and locations 0, 1, ..., last of y contain digits, the call "numerate(y,last)" returns an int comprised of the digits in y in locations 0 through last. Thus, if last=2 and y=[2,3,2,3,4] then "numerate(y,2)" returns the value 232. int numerate(int y[],int last) {int temp; temp=0; for(int j=0;j<=last;j++) temp=10*temp+y[j]; return temp; }; 12. (10 pts)Given the function and variable declarations below, state what output is generated by the code below the dashed line. void b(int & x, int &y) {x++; y+=x; cout <