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 "<next!=NULL) root=root->next; root->next=temp; } 4. Assume the class BiNode below is used to construct a binary tree. Write a function INTREE which returns true if and only if a given entry or its negative is in the tree. If ROOT is of type *BiNode and N is of type int the call INTREE(ROOT, N) would return true if and only if the value N or -N is in the tree. class BiNode{ public: int key; BiNode *child[2]; }; bool INTREE(BiNode *rt,int n){ if(rt==NULL) return false; return rt->key==n || rt->key==-n || INTREE(rt->child[0],n) || INTREE(rt->child[1],n); } 5. This is a non-recursive variation of question 2. Write a single (template) function MATCH which determines whether two arrays match in the way described in question 2. If we have int a[20],b[20]; double x[30], y[50]; and Link u[10],v[30]; then each of the calls MATCH(a,b,2,10), MATCH(x,y,4,12), and MATCH(u,v,5 13) should return true if and only if the corresponding arrays have the same entries in the same locations. DO NOT USE RECURSION FOR THIS QUESTION. template bool MATCH(T a[], T b[],int low, int high){ bool temp; temp=true; while(temp && low<=high){ temp=temp && a[low]==b[low]; low++; } return temp; } 6. The selection sort takes the largest entry in an array and puts it in the first location. Then it takes the largest of the remaining entries and puts it in the second location, etc. Use this algorithm to write the function SORT. If we have double x[300]; and int n; then the entries x[0]...x[n-1] should be in descending order after the call SORT(x,n). void SORT(double x[],int n){ int maxLoc,temp; for(int i=0; ix[maxLoc]) maxLoc=loc; temp=x[maxLoc]; x[maxLoc]=x[i]; x[i]=temp; } } 7. (a) Write a function DUPLICATE which returns true if and only if any two entries in an array are the same. If we have x and n declared as in question 6, then the call to DUPLICATE would be DUPLICATE(x,n). It would return true if any two of the entries x[0]...x[n-1] are the same. bool DUPLICATE(double x[],int n){ int outer, inner; bool temp; temp=false; outer=0; while(outer>temp; while(fin.good()){ st.push(temp); q.enqueue(temp); fin>>temp; } while(!q.empty()) cout<='A' && ch<='Z'; getch(fin,ch); j++; } ok=ok && ch=='\n'; while(ch!='\n') getch(fin,ch); if(!ok) cout<<'*'; cout< 40 30 / \ / \ / \ / \ 18 16 28 26 38 16 28 26 / / 60 18 (ii) Using the algirthms discussed in class for maintaining a heap, show the heap above after the number 40 has been removed it. 38 / \ / \ 38 30 ==> 18 30 / \ / \ / \ / \ 18 16 28 26 16 28 26 38 38 / \ / \ ==> 18 30 ==> 26 30 / \ / / \ / 26 16 28 18 16 28 (b) Suppose we have the following tree in which the numbers are entered so that balance is maintained and so that an in-order traversal of the tree lists the entries in ascending order. 15 / \ 7 17 / \ / \ 5 10 16 18 / \ / \ 4 6 9 11 (i) Using the algorithms discussed in class for maintaining balance, show the tree above after the number 3 is added to it. 15 7 / \ / \ 7 17 5 15 / \ / \ ==> / \ / \ 5 10 16 18 4 6 10 17 / \ / \ / / \ / \ 4 6 9 11 3 9 11 16 18 / 3 (ii) Using the algorithms discussed in class for maintaining balance, show the tree above after the number 12 is added to it. 15 10 / \ / \ 7 17 7 15 / \ / \ ==> / \ / \ 5 10 16 18 5 9 11 17 / \ / \ / \ \ / \ 4 6 9 11 4 6 12 16 18 \ 12