c17f01f - CSc 17 Final Examination Tuesday 18 December 2001...

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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).
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern