Programming Assignments for Chapter 8

  1. Complete the functions: printBox and main (marked with "Fill in here"). Function printBox prints a box whose height is half the width with signs and width specified by the users from keyboard.
    void  printBox(int numSigns, char sign);
    
    int main ()
    {
      int  number;
      char symbol;
    
      cout << "Enter the number of signs for the box; "
           << "press return." << endl;
      cin >> number;
    
      cout << "Enter the sign for the box; "
           << "press return." << endl;
      cin >> symbol;
    
      //Fill in here...
      
      return 0;
    }
     
    //Print a box with characters of "sign" of numSigns x numSigns/2  
    void  printBox(int numSigns, char sign)
    {
       //Fill in here
    }
    
    
    Enter the number of signs for the box; press return.
    20
    Enter the sign for the box; press return.
    $
    $$$$$$$$$$$$$$$$$$$$
    $                  $
    $                  $
    $                  $
    $                  $
    $                  $
    $                  $
    $                  $
    $                  $
    $$$$$$$$$$$$$$$$$$$$
    Press any key to continue . . .
    
    Enter the number of signs for the box; press return.
    30
    Enter the sign for the box; press return.
    #
    ##############################
    #                            #
    #                            #
    #                            #
    #                            #
    #                            #
    #                            #
    #                            #
    #                            #
    #                            #
    #                            #
    #                            #
    #                            #
    #                            #
    ##############################
    Press any key to continue . . .
    
    
  2. Suppose an alien giant walks with decreasing steps (stride length). Each step is half of his last step. Write a function to calculate how far the alien giant can go with known steps if the giant's very step is one mile. Test your program with different numbers of steps to observe the results. With how many steps can the alien giant go more than two miles? Extra points will be given if you can find the result mathematically.
    (Hint: Total distance = 1 + 1/2 + 1/4 + 1/8 + ...)
    double walk(int steps)
    {
       double totalDistance;
       //Fill in here
        
       return totalDistance;
    }
    
    int main()
    {
       //Sample function calls. You might change the number of steps to test how far the giant can go
       //Use setprecision(16) and setw() for formatting the output.
       
       for (int i = 0; i < 30; i++)
       {
       }
       
       return 0:
    }
    
    //Sample output:
    
    With    1 step  the giant walked   1.0000000000000000 miles.
    With    2 steps the giant walked   1.5000000000000000 miles.
    With    3 steps the giant walked   1.7500000000000000 miles.
    With    4 steps the giant walked   1.8750000000000000 miles.
    With    5 steps the giant walked   1.9375000000000000 miles.
    With    6 steps the giant walked   1.9687500000000000 miles.
    With    7 steps the giant walked   1.9843750000000000 miles.
    With    8 steps the giant walked   1.9921875000000000 miles.
    With    9 steps the giant walked   1.9960937500000000 miles.
    With   10 steps the giant walked   1.9980468750000000 miles.
    With   11 steps the giant walked   1.9990234375000000 miles.
    With   12 steps the giant walked   1.9995117187500000 miles.
    With   13 steps the giant walked   1.9997558593750000 miles.
    With   14 steps the giant walked   1.9998779296875000 miles.
    With   15 steps the giant walked   1.9999389648437500 miles.
    With   16 steps the giant walked   1.9999694824218750 miles.
    With   17 steps the giant walked   1.9999847412109375 miles.
    With   18 steps the giant walked   1.9999923706054688 miles.
    With   19 steps the giant walked   1.9999961853027344 miles.
    With   20 steps the giant walked   1.9999980926513672 miles.
    With   21 steps the giant walked   1.9999990463256836 miles.
    With   22 steps the giant walked   1.9999995231628418 miles.
    With   23 steps the giant walked   1.9999997615814209 miles.
    With   24 steps the giant walked   1.9999998807907104 miles.
    With   25 steps the giant walked   1.9999999403953552 miles.
    With   26 steps the giant walked   1.9999999701976776 miles.
    With   27 steps the giant walked   1.9999999850988388 miles.
    With   28 steps the giant walked   1.9999999925494194 miles.
    With   29 steps the giant walked   1.9999999962747097 miles.
    With   30 steps the giant walked   1.9999999981373549 miles.
    
  3. Write a program to find the maximum, minimum, mean, and standard deviation from the following data set. You need to write a function that takes an ifstream variable as "input" parameter and "returns" the these values as output. A skeleton program can be found at the course OneDrive share inside a folder called p7_functions.
      8   7  19  14  21  22   6  19  45   8
     36   7  43  42   9  30  11  45  45  26
     14  45  45   6  30  45  12  45  31  47
      3  40  18  20  12  49  22  33  36  27
    
    Hint: consider using passing-by-reference method to mimic returning multiple values to the function caller. For example:
    void getStatistics(ifstream & inData, int & max, int & min, double & mean, double & std)
    
    Reset the ifstream to the beginning of a file again:
      //ifstream inData;
      inData.clear();
      inData.seekg(0, ios::beg);
    
  4. Submit your programs and outputs to Blackboard.