Part A Write a program that reads a specified number of floating point numbers from the user and prints their average. The first number input by the user will be an integer that specifies how many numbers are to be averaged. The rest of the input will be the floating point numbers to be averaged. Your output should be accurate to three decimal places. For example, if the input is: 8 1.39 5.5 9.6885 3.198 23.58684 17 -6.58 16.35 your output should be: average = 8.767. You must use a for loop to solve this problem. Part B Add the following two features to the above problem: 1. the user is able to enter an arbitrary number of data sets, and you need to average each data set. Each data set starts with the number of observations, entered as floating point numbers, and then the observations. When you encounter a data set that starts with the number 0, you will exit the program. You will need to add an outer while loop (i.e., a while loop that surrounds your for loop) to your program. 2. the size of the data set must be between 1 and 10 inclusive. You should continually prompt the user for a valid data size until the user enters either 0, meaning that you should exit the program, or an integer between 1 and 10. Use a do while to implement this prompt (you may need to put this prompt in two different places in your program). For example, if the input is: 8 1.39 5.5 9.6885 3.198 23.58684 17 -6.58 16.35 1 17.85 3 10 9 4 0 then your output should be average = 8.767 average = 17.850 average = 7.667 Since each average will print as soon as the dataset for that average has been entered, your output will not look this nice, because your output will be interspersed with your input. Part C You will add two more features to your program in this extra credit part: 1. In addition to printing your output, you should now graph your output as well by printing an appropriate number of asterisks (‘*’). Since you cannot print a fractional asterisk, you will round your average to the nearest integer. You can do this by adding 0.5 to your average and then assigning your number to an integer variable. C++ will automatically truncate the decimal part of the average when you do so. For example, if your average is 7.667, then adding 0.5 to 7.667 will yield 8.167, which will get truncated to 8. 2. At the end of your program, print the smallest and largest of the averages for your data sets, and the average of your averages, all to three decimal places. The average of your averages should be the sum of the averages divided by the number of data sets. When you are trying to compute the smallest and largest averages, you will run into a problem of how to initialize the variables that keep track of the smallest and largest averages. Since you do not have a bound of how large or small the averages can be, you cannot try assigning really large or really small numbers to these variables. My suggestion is to declare a boolean variable that keeps track of whether this is the first average your program has computed. If it is true, then assign your computed average to both of your tracking variables. If it is false, then check to see whether this computed average is a new minimum or maximum average and take the appropriate action. For the input shown in problem 2, your output should now be: 8.767 ********* 17.850 ****************** 7.667 ******** smallest average = 7.667 largest average = 17.850 average of averages = 11.428 |
|
|
|
|
|
|