c++ nested loops

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 results for each experiment. Display the average with a precision of two digits after the decimal point.
experiment 1 results = 23.2; 31; 16.9; 27; 25.4
experiment 2 results = 34.8; 45.2; 27.9; 36.8; 33.4
experiment 3 results = 19.4; 16.8; 10.2; 20.8; 18.9
experiment 4 results = 36.9; 39; 49.2; 45.1; 42.7

Because of the extraneous nested loop requirement, instead of doing it the normal way, you can do something pointless like iterate over all the experiments, the iterate over each of those inside to compute the averages.

so

for (all the experiments)
for(all the data in that experiment)
{
...add up and divide by # etc average code here
}

and so on.

Hi im struggling with the same problem any assistance please
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

float Average(const vector<float>& v)
{
    float sum = 0.0f;
    for (auto& f : v)
        sum += f;
    return sum / v.size();
}

int main() 
{
    vector<vector<float>> all_experiments = 
    {
        {23.2, 31, 16.9, 27, 25.4},
        {34.8, 45.2, 27.9, 36.8, 33.4},
        {19.4, 16.8, 10.2, 20.8, 18.9},
        {36.9, 39, 49.2, 45.1, 42.7}
    };
    
    cout << fixed << setprecision(2);
    for (int i=0; i<all_experiments.size(); ++i)
        cout << (i+1) << ": " << Average(all_experiments[i]) << endl;
}


1: 24.70
2: 35.62
3: 17.22
4: 42.58
Last edited on
thank you so much guys.
Last edited on
@icy: don't help him/her by doing all his/her hw
chipp, really, it's no trouble.
chipp, really, it's no trouble.


it is a problem. this is not a web for doing some1's hw
Topic archived. No new replies allowed.