Lab 4 - Introduction to Functions

In this lab, you will be learning how to make function calls using the math library cmath to perform basic statistical calculations.

The first part of the lab will be a brief lecture about the nature of functions and function calls. Then you will complete a short programming assignment that calls functions in the cmath library.

The program will ask the user for three doubles and calculate the average and standard deviation for the entered values. You must first calculate and store the average in a variable. Then calculate the standard deviation according to the following formula (assuming the average is stored in a variable named avg and the three numbers are stored in variables named num1, num2, num3):

deviation = sqrt( ((num1 - avg)2 + (num2 - avg)2 + (num3 - avg)2) / 2)

The tricky part on this equation is making sure the parentheses are in the appropriate place when you translate it into code. You must sum all the squared errors before dividing by 2. You must divide by 2 before calling sqrt(). If you do not do the operations in the correct order, the calculated value will not be the actual standard deviation. You may wish to paste the above function into vi so you can use syntax highlighting to visualize the matching parentheses.

You will need to call the sqrt() library function and the pow() library function to perform this calculation. Do not multiply a value by itself to calculate the square, use the pow() function call instead. Assignments will be marked down if they do not call pow().

Embed the code that prompts the user, reads the doubles and calculates the average and standard deviation into the body of a do-while loop which will prompt the user if they wish to enter another set of data after performing the calculations. The loop will continue to the next iteration if the user enters anything other than 'n' or 'N' (even if they hit 'u' instead of 'y' for example). This is another form of user-driven loops (instead of menus), where the number of iterations is determined by the user.

Your output should appear similar to the following:

Enter 3 doubles: 3.5 5.3 1.2
The average is 3.33333
The standard deviation is 2.05508
Do you wish to enter another set of data? (Y|n) y

Enter 3 doubles: 6.5 2.2 9.8
The average is 6.16667
The standard deviation is 3.81095
Do you wish to enter another set of data? (Y|n) n

Goodbye!
Name your source code lab4.cpp. Email me the completed source code.