Programming Assignments for Chapter 8

  1. Complete the functions: print and main. Function print prints a rectangle with a rectangular hole whose height, width, and the sign are specified by the users from keyboard.
    //Skeleton program int main ()
    {
      //Ask the user to specify the height and width of a rectangle as well as a symbol for it.
      //Call the print function to print a rectangle with a rectangular hole.
    
      return 0;
    }
    
    void print(int height, int width, char sign)
    {
      //Print a rectangle with a rectangular hole whose size is half of the containing rectangle.
      //The hole is at the center of the containing rectangle.
    }
    
    //Sample output with two test cases:
    Enter a height and a width for the rectangle:
    16 20
    Enter a symbol for the the rectangle:
    #
    # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # #
    # # # # # #                   # # # # # #
    # # # # # #                   # # # # # #
    # # # # # #                   # # # # # #
    # # # # # #                   # # # # # #
    # # # # # #                   # # # # # #
    # # # # # #                   # # # # # #
    # # # # # #                   # # # # # #
    # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # #
    
    Enter a height and a width for the rectangle:
    20 36
    Enter a symbol for the rectangle:
    0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0                                   0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0                                   0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0                                   0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0                                   0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0                                   0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0                                   0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0                                   0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0                                   0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0                                   0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    
    
  2. Complete the functions: printGrid and main. Function printGrid prints a grid whose height, width, grid line spacing in horizontal and vertical directions, and the sign for grid lines are specified by the users from keyboard.
    //Skeleton program
    int main ()
    {
      //Ask the user to provide the height, width, the grid line spacing (the number of cells in horizontal and vertical directions), and the sign for the grid lines.
      //Call the printGrid function to print a grid.
     
      return 0;
    }
     
    void printGrid(int height, int width, int hCells, int vCells, char sign)
    {
      //Print a grid
    }
    
    //Sample output with two test cases:
    Enter the height and width of a grid:
    20 30
    Enter the number of horizontal cells for the grid:
    5
    Enter the number of vertical cells for the grid:
    5
    Enter the sign for the grid lines:
    +
    
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +           +           +           +           +           +
    +           +           +           +           +           +
    +           +           +           +           +           +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +           +           +           +           +           +
    +           +           +           +           +           +
    +           +           +           +           +           +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +           +           +           +           +           +
    +           +           +           +           +           +
    +           +           +           +           +           +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +           +           +           +           +           +
    +           +           +           +           +           +
    +           +           +           +           +           +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +           +           +           +           +           +
    +           +           +           +           +           +
    +           +           +           +           +           +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    
    Enter the height and width of a grid:
    16 20
    Enter the number of horizontal cells for the grid:
    4
    Enter the number of vertical cells for the grid:
    2
    Enter the sign for the grid lines:
    #
    
    # # # # # # # # # # # # # # # # # # # # #
    #                   #                   #
    #                   #                   #
    #                   #                   #
    # # # # # # # # # # # # # # # # # # # # #
    #                   #                   #
    #                   #                   #
    #                   #                   #
    # # # # # # # # # # # # # # # # # # # # #
    #                   #                   #
    #                   #                   #
    #                   #                   #
    # # # # # # # # # # # # # # # # # # # # #
    #                   #                   #
    #                   #                   #
    #                   #                   #
    # # # # # # # # # # # # # # # # # # # # #
    
    
  3. Suppose an alien giant walks with decreasing steps (stride length). Each step is half of his last step. Write a function to calculate the total distance the alien giant can go with known steps if the giant's very first step is one mile. Test your program with different numbers of steps to observe the results. Extra points will be given if you can find the result mathematically.
    (Hint: Total distance = 1 + 1/2 + 1/4 + 1/8 + ...)
    //Calculate the total distance the giant can walk with known steps.
    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
       //cout << fixed << showpoint; to print in decimal format instead of scientific notation
       //Use setprecision(16) and setw() for formatting the output.
       
       for (int i = 0; i < 30; i++)
       {
          //Call the walk function with i steps.
       }
       
       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.
    
  4. 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" these values as output. A skeleton program can be found 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);
    
  5. Write a program to find the maximum, minimum, mean, and standard deviation Links to an external site. from the following data set. You need to write a function that takes an ifstream variable as "input" parameter and "returns" these values as output using passing by reference. The formula for standard deviation is std = sqrt((sum of squares of items)/N - square of the mean). Consider using passing-by-reference method to mimic returning multiple values to the function caller. A skeleton program is given below.
      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
    void stats(ifstream & inData, int& max, int& min, double & mean, double & std);
    
    int main()
    {
    	int max, min;
    	double mean, std;
    
    	ifstream inData;
    	inData.open("input_data.txt");
    
    	stats(inData, max, min, mean, std);
    
    	cout << setw(30) << "Maximum is : "  << setw(20) << max << endl;
    	cout << setw(30) << "Minimum is : " << setw(20) << min << endl;
    	cout << setw(30) << "Mean is : " << setw(20) << mean << endl;
    	cout << setw(30) << "Standard Deviation is : " << setw(20) << std << endl;
    
    	return 0;
    }
    
    void stats(ifstream & inData, int & max, int & min, double & mean, double & std)
    {
    	//Use a while loop to read data from the file and calculate the stats.
    }
    
  6. Write a function that finds the area and circumference of a circle, given the radius of the circle. The function needs to be called from a main function with a radius of 10.0. Note that the function takes radius as input and "produces" both area and circumference as output. (Hint: consider using passing-by-reference method. const double PI =3.141592653;)
  7. Submit your programs and outputs to Canvas.