Chapter 5
Functions for All Subtasks
1. Solutions to Selected Practice Programs and Programming Projects Detailed solutions selected projects are presented here. The rest are essentially the same problems, except for what is being converted. Notes about the remaining problems are included.
One of the more important things in programming is planning, even for the simplest program. If the planning is thorough, the coding will be easy, and the only errors likely to be encountered are syntax errors, usually caused by either typing errors, a boundary condition problem (frequently, an off by one error), or (we hope not) lack of knowledge of the language details.
Practice Program 1: Statistics
Compute average and standard deviation of 4 entries. General remarks are in the code file which I present here: #include
Task: Write a function that computes average (I will call this the arithmetic mean or simply the mean) and standard deviation of four scores. The average or mean, avg, is computed as
avg = ( s1 + s2 + s3 + s4 ) / 4
The standard deviation is computed as
1
Copyright ? 2018 Pearson Education, Inc.
Savitch
Problem Solving w/ C++, 10e Instructor’s Resource Guide
Chapter 5
where a = avg. Note that some statisticians may wish to use 3 instead of 4. We will use 4.
Input: scores s1 s2 s3 s4
Output: standard deviation and mean.
Required: The function is to have 6 parameters. This function calls two others that compute the mean and the std deviation. A driver with a loop should be written to test the function at the user's option. */
//function declaration (or prototype)
//When used, the math library must be linked to the //executable.
#include
void average (double s1, double s2, double s3, double s4, double& avg) {
avg = ( s1 + s2 + s3 + s4 ) / 4; }
// Preconditions: average function must have been called on //the data, and the value of the average passed into the //parameter a
( s1 ? a ) ? ( s ) ? ( s3 ? a ) 2 ? a std_deviation = 4
14
Copyright ? 2018 Pearson Education, Inc.
Savitch
Problem Solving w/ C++, 10e Instructor’s Resource Guide
Chapter 5
//Postconditions: Standard deviation is passed back in //the variable stdDev
void sD (double s1, double s2, double s3, double s4, double a, double& stdDev) {
stdDev = sqrt( (s1 - a)*(s1 - a) + (s2 - a)*(s2 - a) + (s3 - a)*(s3 - a) + (s4 - a)*(s4 - a) )/4 ; }
void statistics( double s1, double s2, double s3, double s4, double& avg, double& stdDev );
//Preconditions: this function is called with any set of //values. Very large or very small numbers are subject to //errors in the calculation. Analysis of this sort of error //is beyond the scope of this chapter.
//PostConditions: avg is set to the mean of s1, s2, s3, s4 //and stdDev is set to the standard deviation of s1..s4 //function definition:
void statistics( double s1, double s2, double s3, double s4, double& avg, double& stdDev ) {
average ( s1, s2, s3, s4, avg ); sD ( s1, s2, s3, s4, avg, stdDev ); }
int main() {
double s1, s2, s3, s4, avg, stdDev; char ans;
14
Copyright ? 2018 Pearson Education, Inc.
Savitch
Problem Solving w/ C++, 10e Instructor’s Resource Guide
Chapter 5
do {
cout << \ << \ << endl
<< \ cin >> s1 >> s2 >> s3 >> s4;
statistics( s1, s2, s3, s4, avg, stdDev);
cout << \ << \ << \
<< \
cout << \ cin >> ans;
} while ( 'Y' == ans || 'y' == ans ); return 0; }
A typical run follows: 21:44:35:~/AW$ a.out
Enter 4 decimal numbers, I will give you the mean and standard deviation of the data 12.3 13.4 10.5 9.0
mean of 12.3 13.4 10.5 9 is 11.3
the standard deviation of these numbers is 0.841873 y or Y continues, any other terminates y
Enter 4 decimal numbers, I will give you the mean and standard deviation of the data 1 2 3 4
14
Copyright ? 2018 Pearson Education, Inc.
Savitch
Problem Solving w/ C++, 10e Instructor’s Resource Guide
Chapter 5
mean of 1 2 3 4 is 2.5
the standard deviation of these numbers is 0.559017 y or Y continues, any other terminates n
21:45:05:~/AW$
Practice Program 2: Convert Feet/Inches to Meters Conversion of feet/inches to meters:
//Task: Convert feet/inches to meters
//Input:a length in feet and inches, with possible decimal //part of inches
//Output: a length in meters, with 2 decimal places, which //are the 'centimeters' specified in the problem.
//Required: functions for input, computation, and output. //Include a loop to repeat the calculation at the user's //option.Remarks: The computation is a simple conversion from //feet + inches to feet with a decimal part, then to meters. //Output is restricted to 2 decimal places. //
//By 'meters and centimeters' the author means that the //output is to be meters with two decimal places - which is //meters and centimeters. I mention this because my students //always stumble at this because of a lack of knowledge of //the metric system. #include
void input ( int& feet, double& inches ); //Precondition: function is called
14
Copyright ? 2018 Pearson Education, Inc.