{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CSC 17 Final Examination FALL 2001

# CSC 17 Final Examination FALL 2001 - CSc 17 Final...

This preview shows pages 1–3. Sign up to view the full content.

CSc 17 Final Examination Tuesday 18 December 2001 >>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 1. (25 pts) The code below will compile but not link, because the function f() is not defined. Add code for a single function f() to the code below so that the code compiles and runs and produces the output between the dashed lines. #include <fstream.h> void a(int x) {cout<<(x*x)<<endl;} void b(double y) {cout<<y<<endl;} int main() {f(a,3); f(b,2.5); } /*----------------------- 9 2.5 --------------------------*/ -------ANSWER template <typename T, typename V> void f(T g,V x){ g(x); } 2. (25 pts) Assume that the class Node below is used to build a binary tree. Further assume that as the tree is built the height of each Node is stored in the data member 'height'. Write a function balanced() which returns 'true' if and only if the tree is balanced. If rt is declared as Node *rt, the call to determine whether the binary tree with root rt is balanced would be balanced(rt). class Node {public: double data; int height; Node *child[2]; }; ANSWER ------------- int h(Node *rt) {if(rt==NULL) return -1; return rt->height; } bool balanced(Node *rt) {if(rt==NULL) return true; int lHt,rHt; bool tBal; lHt=h(rt->child[0]); rHt=h(rt->child[1]); tBal=lHt-rHt<2 && rHt-lHt<2; return balanced(rt->child[0]) && balanced(rt->child[1]) && tBal; }

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

View Full Document
3. (25 pts) Write a function, g, which reads text from a file f, of type istream, and displays a faithful copy of the file on the screen, except that it appends an asterisk (*) after every line whose first character is a digit (0, 1, ..., 9) and whose last character is a period (.). The call to the function would read g(f).
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

CSC 17 Final Examination FALL 2001 - CSc 17 Final...

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

View Full Document
Ask a homework question - tutors are online