c17f01f - CSc 17 Final Examination Tuesday 18 December

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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; } 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)....
View Full Document

This note was uploaded on 03/09/2008 for the course CSE 17 taught by Professor Varies during the Spring '08 term at Lehigh University .

Page1 / 5

c17f01f - CSc 17 Final Examination Tuesday 18 December

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