Name: __________________

 

Quiz 6 (Week 7)

CS-150, Fall ‘02, Dr. C. S. Tritt

 

Each of the following 2 questions is worth the same amount.

 

1.      What output would the following fragment of C++ code produce (assume any necessary header files have been included and variables declared and defined)?

 

double pole(double a, double b, double c, bool& f)

{

   if (b == c)

   {

      f = false;

      return 0.0;

   }

   else

   {

      f = true;

      return a/(b-c);

   }

}

 

int main()

{

   bool flag;

   double x = 3.;

   double y = 2.;

   double z = 1.;

   cout << "Working... ";

   double result = pole(x, y, z, flag);

   if (flag) cout << "Result  1 is " << result << endl;

   z = 2.;

   cout << "Working... ";

   result = pole(x, y, z, flag);

   if (flag) cout << "Result  1 is " << result << endl;

   cout << "All done.\n";

   return 0;

}

 

Answer –

 

Working... Result  1 is 3

Working... All done.

 

Display second result –15, forgetting All done –5, displaying Result… true or false –25, new lines not counted.

 

2.      Write a fragment of C++ code that defines a type double function called poly that evaluates the expression y(x) = x2 + x – 1 where x is a type double. For example, the call poly(3.0) would return the value 11.0.

 

Answer –

 

double poly(double x)

{

   return x*x + x - 1;

}

 

Defining variables twice –5, not returning results –10, I/O in function –20, extra arguments –10 and returning wrong value –5.