c++ nested loops

Apr 11, 2018 at 12:52pm
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

Apr 11, 2018 at 1:53pm
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.

Apr 11, 2018 at 8:55pm
Hi im struggling with the same problem any assistance please
Apr 11, 2018 at 9:26pm
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 Apr 11, 2018 at 9:27pm
Apr 12, 2018 at 11:31am
thank you so much guys.
Last edited on Apr 12, 2018 at 11:43am
Apr 13, 2018 at 9:17am
@icy: don't help him/her by doing all his/her hw
Apr 13, 2018 at 2:37pm
chipp, really, it's no trouble.
Apr 15, 2018 at 3:50pm
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.