CSc 17 Final Examination 28 July 2002 1. (25 pts) Write a function numberLines() which reads text from a file and faithfully echoes it to the screen, except that it appends the line number at the end of each line in the form [xx]. For example, if the file containing the text is stored in "final.txt", the first two line of output produced by the code below would be: CSc 17 Final Examination 28 July 2002 [1] [2] ifstream out("final.txt"); numberLines(out); --------------------------------------------------- void nextCh(istream &inp,char &ch) {inp.get(ch); if(!inp.good()) ch='\n'; } void numberLines(istream &inp) {int count; char ch; nextCh(inp,ch); count=0; while(inp.good()) {while(ch!='\n') {cout<=0. For example, for the following code, the output would be "no yes no ". int x[]={1,7,-8,15,20,30}; double y[]={-1.7, 2.7, 3.7, 4.7}; if( posArray(1,4,x)) cout<<"yes "; else cout<<"no "; if(posArray(3,5,x)) cout<<"yes "; else cout<<"no "; if(posArray(0,3,y)) cout<<"yes "; //note the call with parameter y else cout<<"no "; -------------------------------------- template bool posArray(int low, int high, T x[]) {while(high>=low) if(x[high]<0) return false; else high--; return true; } 4, (25 pts) Write a recursive function, sum(), which recursively finds the sum of the entries of an array of ints between two indices inclusive, using the following algorithm. Divide the given section of the array in half, find the sum of each half, and return the sum of the two resulting sums. For example, the output of the following code would be "The sum is 32". int x[]={2, 3, 14, 15, 1, 1, 1, 6, 7}; cout<<"The sum is "<high) return 0; if(low==high) return x[low]; return sum(low,(low+high)/2,x)+sum((low+high)/2+1,high,x); } 5. (25 pts) Imagine the following Abstract Data Type which has both the properties of a stack and of a queue and which some people call a "Staque". An int can be added to the front (addFirst()) or the end (addLast()), or removed from the front (removeFirst()). Write the declaration and code for a class Staque which implements this Abstract Data Type. The code below should produce the output: 3 2 4 Staque st; st.addFirst(2); st.addFirst(3); st.addLast(4); cout<next()!=NULL) temp=temp->next(); temp->setNext(new Node(x)); assert(temp->next()!=NULL," Heap overflow"); } } int Staque::removeFirst() {Node *temp; int num; assert(!empty(),"Heap underflow"); temp=front; front=front->next(); num=temp->getData(); delete temp; return num; } bool Staque::empty() {return front==NULL;} void Staque::assert(bool b,char *mess) {if(!b) {cerr<<"ERROR[Staque]: "< "; cin.get(); cin.get(); exit(1); } } 6. (25 pts) Assume the class Node from question 5. Write a non-member function enumerate() which returns the number of times an int appears in a linked list. For example, if we have Node *head; and head points to the first entry of the linked list 2->3->5->2->30->2 then the code below would produce the output: "0 3 1". cout<getData()==x) count++; front=front->next(); } return count; } 7. (25 pts) For the class Node of question 5, overload the operators "<" and "<<" so that the code below produces the output "[5]->[6]-> Yes1 Yes2 Yes3 Yes4 Yes5 Yes6" Node x(5),y(6); cout<"; return out; } 8. (25 pts) Fully document the following code: class A {public: A(); A(int j); void one(int j); int two(); private: bool x; int d; static void assert(bool b, char *mess); }; A::A(){x=false;} A::A(int j){d=j; x=true;} void A::one(int j){d=j; x=true;} int two(){assert(x," Programmer: replace this message"); return d; } void A::assert(bool b, char *mess) {if(!b) {cerr<<"ERROR[A]: "< to quit program.. "; exit(1); } } -------------------------------------------------- class A //a class to prevent accessing an int before it has been //assigned a value {public: A(); //create an instance but have the variable //be undefined A(int j); //assign variable the value j and set //"defined" to true void one(int j); //assign variable the value j and set //"defined" true int two(); //if the variable is defined return its //value. If undefined, diagnose error and //quit program private: bool x; //x==true if and only if the variable has //been assigned a value int d; //for storing the value of the variable static void assert(bool b, char *mess); //precondition: mess terminates with \0 //if b is false, display an error message and exit program //if b is true, do nothing }; A::A(){x=false;} A::A(int j){d=j; x=true;} void A::one(int j){d=j; x=true;} //replace the string below with "Programmer error: Accessing undefined variable" int two(){assert(x," Programmer: replace this message"); return d; } void A::assert(bool b, char *mess) {if(!b) {cerr<<"ERROR[A]: "< to quit program.. "; exit(1); } }