Take-Home Quiz 3 Solutions

(each question is worth 2 pts)

 

 

  1. Using dots to represent rectangles (don’t worry about the color), draw the shape that the following segment of code displays.
  2. 
    #include "rect.h"SimpleWindow MyWin("My shape", 17.0, 9.0);
    int ApiMain() {
         MyWin.Open() ;
         // Draw a rectangle made up of red and blue squares
         for (int i=1; i<=5; i++) {
            for (int j=1; j<=5; j++) {
               if ( j >= i) {
                 RectangleShape R1(MyWin,  i, j, Blue, 0.75, 0.75) ;
                 R1.Draw();
               }
            }
         }  
        return 0 ;
    }
    

     

      .
      . .
      . . .
      . . . .
      . . . . .
    

     

  3. What will the following segment of code print?
  4. 
    #include <iostream>
    using namespace std;
    int main(){   
       for (int count = 1; count < 10; count++) { 
         cout << "counting backwards to 1 starting with " << count << ": ";
         switch (count) {       
             case 10:
                cout << "ten ";
             case 9:
                cout << "nine ";
            case 8:
                cout << "eight ";
             case 7:
               cout << "seven ";
             case 6:
                cout << "six ";
             case 5:
                cout << "five ";
             case 4:
                cout << "four ";
             case 3:
                cout << "three ";
             case 2:
                cout << "two ";
             case 1:
                cout << "one" << endl;
          }
       }
       return 0;
    }
    

     

     
    When the missing bracket is added as shown above the output is:
    
    counting backwards to 1 starting with 1: one
    counting backwards to 1 starting with 2: two one
    counting backwards to 1 starting with 3: three two one
    counting backwards to 1 starting with 4: four three two one
    counting backwards to 1 starting with 5: five four three two one
    counting backwards to 1 starting with 6: six five four three two one
    counting backwards to 1 starting with 7: seven six five four three two one
    counting backwards to 1 starting with 8: eight seven six five four three two one
    counting backwards to 1 starting with 9: nine eight seven six five four three two one
    

     

  5. Write a for loop that calculates the sum of the numbers 1 through 10.
  6.  

    
      for(int i = 1, int sum = 0; i < 11; i++) 
         sum += i;
    

     

  7. Write a function that takes two integer numbers as input and returns their sum.
  8.  

    
      int sum (int a, int b)
      {
         return a+b;
      }
    

     

  9. Write a program that prompts the user to enter a positive integer number, computes the square root of that number, and then prints the square root of that number to the monitor.
  10.  

    
    #include <fstream.h>
    #include <math.h>
    int main() {
       cout << "Enter a positive integer" << endl;
       int Value = 0;
       cin >> Value;
       cout << "The square root of that integer is: " << sqrt(Value) << endl;
       return 0;
    }
    

     

  11. Write a while loop that prompts the user to enter an integer number that is greater than 0 and less than 10 until the user does actually enter a number that matches those requirements.
  12.  

    
      int value = 0;
      while(value < 1 || value > 9) {
            cout << "Enter a integer between 1 and 9" << endl;
            cin >> value;
      }
    

     

  13. Explain what the line of code pointed to by 1 in the program below is doing.
  14. 
    <#include <fstream.h>
    int main() {
       ifstream fin("mydata.txt");   ç== 1
       int ValuesProcessed = 0;
       float ValueSum = 0;
       float Value;
       while (fin >> Value) {
          ValueSum += Value;
          ++ValuesProcessed;
       }
       if (ValuesProcessed > 0) {
          ofstream fout("average.txt");
          float Average = ValueSum / ValuesProcessed;
          fout << "Average: " << Average << endl;
       }
       else
          cerr << "No list to average" << endl;
       return 0;
    }
    

     

    An object of type ifstream is declared.  The identifier for that object is fin 
    and it opens the file mydata.txt for reading.
    

     

  15. What will the program below print?
  16. 
    #include <iostream.h>
    void Swap(int Value1, int Value2) {
       int Temp = Value1;
       Value1 = Value2;
       Value2 = Temp;
       return;
    }
    
    int main() {
       int Number1 = 4;
       int Number2 = 3;
       if (Number1 > Number2) {
          Swap(Number1, Number2);
       }
       cout << "The numbers in sorted order:"
            << Number1 << ", " << Number2 << endl;
       return 0;
    }
    

     

    
      The numbers in sorted order: 4, 3
    

     

  17. What is the data type of the value that will be returned by the function whose prototype is given below? What does this mean?
  18. 
       void fun(int a, int b);
    

     

    The return type void means that the function does not return anything.
    

     

  19. What will be printed by the following code segment:

   #include <iostream.h>
   int i = 10;

   void fun() {
       {       
          int i = 1;
          cout << i << endl; 
          {
              float i = 3.2;
              cout << i << endl;    
              char i = 'a';
              cout << i << endl;    
           }
           cout << i << endl;       
       }
       cout << i << endl;       
   }
There is a compilation error in the above code because the identifier i is declared
twice in the same block.  This question was thrown out.