I have been struggling with this problem and that is why I am asking here for help.
Let's imagine the following set of values for an array called "scores"
20 30 40 50 10 20 40 10
What I want to do is to create a loop that calculates the average each 4 values . In this case and doing manual math it should be
(20+30+40+50)/4
(10+20+40+10)/4
Now , my point and my problem is that I do not know how to do that with a single loop. I understand that I can setup two loops and then do so , but as obvious if the array were bigger then I would have to setup hundreds of loops.Right now the solution I made is very far from the real solution
#include <iostream>
#include <string>
using namespace std;
int main()
{
string names[4] = {"Peter","Mark","July","Sarah"};
double scores [16] = {20,30,100,60,30,40,10,90,10,99,100,12,100,100,10,100};
double average[4] ;
int i;
for (int d = 0, i = 0; d < 4 - 1; d++)
{
average[i] += scores[d];
if (d % 4 == 0) {
average[i] /= 4;
i++;
}
}
for (int l = 0 ; l < 4 ; l++)
{
cout << "\nName : " << names[l] << endl << endl ;
cout << "Average : " << average[l] << endl;
}
}
Desired Output :
Name : Peter Average : 52.5 Name : Mark Average : 42.5 Name : July Average : 55.25 Name : Sarah Average : 77.5
Actual Output : Name : Peter Average : 5 Name : Mark Average : 130 Name : July Average : 3.11076e-317 Name : Sarah Average : 6.90921e-310