CS 1005, C Term 2000
Introduction to Programming
Solution to Test 1

Your Name: _____________________________________

Your Lab Section: ______________     Your Login Name:_________________

Instructions.   Read each problem carefully before answering. Circle the selected answer(s) or write your solutions in ink in the spaces provided when this is the appropriate action as indicated below for each problem. Write neatly. Each problem is worth 60 points. All parts of each problem have equal point value. C++ is the programming language used throughout this test. Good luck!

  1. a) Circle all false statements among the following:
      (Arrows are used instead of circles below to facilitate typesetting)
    
    -->All functions have one or more formal parameters
     
       An object (variable) must be declared before it can be used
    
    -->A loop invariant can be false when the loop test evaluates to false
       
    -->The main() function in a C++ program must have a return type of void
    
       The expression 5*(8/5) evaluates to 5
    
    
    

    b) The following program segment uses three variables. Give declarations for these variables that are consistent with their use in the program segment. No subsequent type conversions should be needed.

       // Place type declarations in the space below
    
       int count;
       string message;
       double j;
    
       for (count=0; count<25; count++) {
          cout << count << endl;
       }
    
       message = "Hello";
    
       j = InputData();  // function prototype is  double InputData();
    
    
    
    
    
    

  2. Given the following C++ source file:
       #include <iostream>
       #include <string>
       using namespace std;
       
       void Inform() {
          cout << "This program performs a simple calculation." << endl;
       }
    
       int GetNumber() {
          string prompt = "Enter a positive integer: ";
          cout << prompt;
          int number;
          cin >> number;
          return number;
       }
       
       int PowerOf2(int target) {
          int power=0;
          int quotient=target;
          while (quotient%2==0) {
             quotient /= 2;
             power++;
          }
          return power;
       }
    
       int main() {
          Inform();
          int n = GetNumber();
          cout << "The exponent of the highest power of 2 that divides " 
               << n << " is " << PowerOf2(n) << endl;
          return 0;
       }
    

    a) What is the return type of the function named GetNumber ?

       Answer: int
    

    b) What is the name of the formal parameter of PowerOf2 ?

       Answer: target
    

    c) Give the names of all external program libraries named explicitly in the program file.

       Answer: iostream, string
    

    d) Write the Boolean expression that is used as a loop test in the body of PowerOf2:

       Answer: (quotient%2==0)
    

    e) Of the four functions defined in the source file, which one executes first when the program is run?

       Answer: main()
    

    f) If the user enters 24 when prompted by the program, what is the output? (Fill in the blanks below.)

       The exponent of the highest power of 2 that divides 24 is 3
    
    
    
    
    

  3. a) Implement the function named PrintStars whose specification is given below.
       void PrintStars(int n)
       // Prints n star (asterisk) characters '*' in a row and then endl
       // Prints only endl if n is not positive
       {
          while (n-- > 0) {
             cout << "*";
          }
          cout << endl;
       }
    
    
    
    
    
    
    
    
    

    b) Using the function PrintStars described in part a) above, write a C++ program segment that prints the output shown below. Your program segment cannot include any explicit insertions to cout, and it must declare all objects used.

       *
       **
       ***
       ****
       *****
    
       for (int k=1; k<=5; k++) {
          PrintStars(k);
       }
    
    
    
    
    
    
    
    
    
    

  4. For each of the following program segments, state what output is produced.
    a) int i=12;
       cout << i%7;     output:  5
    
    
    b) int j=0;
       cout << j++;     output:  0
    
    
    c) int k=1;
       if (k/4 < 0) {
          k+=3;
       }
       else {
          k*=5;
       }
       cout << k;       output:  5
     
    
    
    

  5. Given the function named UFO (Unidentified Function Object) defined below:
       int UFO(int n)
       {
          int stuff=0;
          for (int k=2; k<n; k++) {
             if (n%k == 0) {
                stuff+=k;
             }
          }
          return stuff;
       }
    
    
    a) Give the value returned by the following calls to UFO:
    
       UFO(5);      return value: 0
    
       UFO(10);     return value: 7
    
    

    b) Give a clear and brief description in words of what value is returned when UFO is called with a general positive integer n as the input argument. For example, you could say "the value returned is the result of multiplying n by 3", if that were what UFO actually does.

       The return value is the sum of the (proper) divisors of n.
    
       Equivalently: the return value is the sum of all positive
       integers that divide n, excluding 1 and n itself.
    
    

    c) Suppose that p is a prime number. What value is returned when UFO is called with p as its input argument?

       Answer: 0