CSCI 159 Quiz 2

Name:

The quiz is open notes, open book, and you are permitted to use your linux accounts to write/check practice code if desired. For example, you could use the following to edit, compile, and check code in a quest1.cpp file:
    gedit quest1.cpp &
    g++ quest1.cpp -o quest1x
    ./quest1x
There are three questions and you have 60 minutes to complete the quiz.
The quiz must be completed as a completely individual exercise: you cannot request or accept help from any other source (human or ai) other than the course instructor, and you cannot give help to anyone else for the quiz.
If you finish early you can leave or stay and work quietly while others finish their quizzes.

Question 1: while loops and for loops

Assuming x has already been declared as an integer variable, rewrite the loop below as a while loop, keeping the functionality as close to the original as possible.
for (x = 17; x > 0; x = x - 3) {
    cout << "Current value " << x << ", squared is " << (x*x) << endl;
}

Question 2: do while loops and input error checking

Write a function that gets the user to enter a number greater than 100, error checking for non-numbers and too-small values, repeating until they enter a valid value, and returning the valid value when done.
The function must use a do-while loop for its repetition.
























Question 3: recursion

If the function foo, shown below, is called with a parameter value of 6.0,
(i) What value does it return? (Hint: it's one of 6, 0, -1.5, or 1.5.)
(ii) Show the output it would produce, explain why it shows those values, and explain why it stops (returns) when it does.
float foo(float f)
{
   float R = 1.5;
   if (f > R) {
      cout << f << endl;
      return(foo(f - R));
   }
   return f;
}