Functions

Copyright © 01/25/2000 Carltons' Computer Consulting

This material may be used for public education purposes and may not be used for profit without written consent from Carltons' Computer Consulting.

You will write functions for this assignment, and put them into a single driver program.

Your first function is called stats. Your function has four IN parameters (parameters passed by value) and two OUT parameters (parameters passed by reference), all of type double.

Your second function, sort4, takes four IN/OUT parameters of type double and sorts them into increasing (or at least non-decreasing) order. Your function should call the function swap, conditionally on parameters as follows (say the parameters are s1, s2, s3, s4):

1.swap s1 and s2 if necessary

2.swap s2 and s3 if necessary

3.swap s3 and s4 if necessary

4.swap s1 and s2 if necessary

5.swap s2 and s3 if necessary

6.swap s1 and s2 if necessary

It is necessary to swap two values if the first exceeds the second. Here is a definition of swap that you can use:

void swap( double &a, double &b )

{

double temp = a;

a = b;

b = temp;

}

 

The Driver Program

Your program should repeatedly ask the user for any four real values, which may be entered all on one line. For each set of four numbers: After the four values are read in, you first call stats,

then display the average and standard deviation to the screen after stats returns. After that, you call sort4 on the same four values, and after sort4 returns print out the four variables (the values should now be in increasing order). The user should be allowed the option of quitting the program after processing each set of numbers.

Both of your functions should work on any set of four values, but you should test both your functions specifically on each of the following sets of four values:

s1 s2 s3 s4

1.0 1.0 1.0 1.0

1.0 0.0 -1.0 -2.0

3.5 5.0 4.0 4.5

10.0 6.0 6.5 7.0

 

Write on paper the average and standard deviation for each of the four sets of numbers above.

In addition to your program, also submit a definition of the problem (5%) together with an algorithmic description of the solution (15%), as in parts 1 and 2 of the Software Lifecycle.

Your algorithm should describe how you allow the user to terminate the program gracefully.

 

 

 

 

 

FUNCTION PROTOTYPES

 

void get_input(double& input1, double& input2, double& input3, double& input4);

//Reads four integers from the keyboard.

void swap_values(double& variable1, double& variable2);

// changes the values of variable1 and variable2.

void sort(double& s1, double& s2, double& s3, double& s4);

//Orders the numbers in the variables s1 and s2

//so that after the function call s1 <= s2.

void give_results(double output1, double output2, double output3, double output4, double& sum, double& stdev);

//Outputs the values in output1 and output2.

//Assumes that output1 <= output2

void stats(double st1, double st2, double st3, double st4, double& sum, double& stdev);

//averages 4 integers

//and copmputes the standard deviation.

void get_input(double& input1, double& input2, double& input3, double& input4);

//Reads four integers from the keyboard.

void swap_values(double& variable1, double& variable2);

// changes the values of variable1 and variable2.

void sort(double& s1, double& s2, double& s3, double& s4);

//Orders the numbers in the variables s1 and s2

//so that after the function call s1 <= s2.

void give_results(double output1, double output2, double output3, double output4, double& sum, double& stdev);

//Outputs the values in output1 and output2.

//Assumes that output1 <= output2

void stats(double st1, double st2, double st3, double st4, double& sum, double& stdev);

//averages 4 integers

//and computes the standard deviation.

Copyright © 01/20/2000 Carltons' Computer Consulting

This material may be used for public education purposes and may not be used for profit without written consent from Carltons' Computer Consulting