Four experiments are performed, each consisting of five test results. The results for each experiment are given in the following list. Write a program using a nested loop to compute and display the average of the test results for each experiment. Display the average with a precision of two digits after the decimal point.
1st experiment results: 23.2 31 16.9 27 25.4
2nd experiment results: 34.8 45.2 27.9 36.8 33.4
3rd experiment results: 19.4 16.8 10.2 20.8 18.9
4th experiment results: 36.9 39 49.2 45.1 42.7
Use the input provided in the given list and execute the program.
1
Expert's answer
2018-05-11T04:30:25-0400
#include <iostream> #include <vector> #include <iomanip> int main() { const int countOfExperiments = 4; const int countOfResults = 5; std::vector< float > averageValues(countOfExperiments); for(int i = 1; i <= countOfExperiments; ++i) { std::cout << "Experiment #" << i << std::endl; for(int j = 1; j <= countOfResults; ++j) { float value; std::cout << "Enter result #" << j << ' '; std::cin >> value; averageValues[i - 1] += value; } averageValues[i - 1] /= countOfResults; } for(int i = 0; i < countOfExperiments; ++i) { std::cout << "Average value for experiment #" << i + 1 << " - " << std::fixed << std::setprecision(2) << averageValues[i] << std::endl; } return 0; }
Comments
Leave a comment