A moving average is the average of the last n data items. For example if you have the
numbers 1, 2, 3, 4, 5, 6, 7 then the moving average of size 4 is (1+2+3+4)/4 = 2.5, then
(2+3+4+5)/4 = 3.5, then (3+4+5+6)/4 = 4.5 etc.
Anyone knows how to write a for loop to find the average with window size of 4 ?
let say 1,2,3,4,5,6,7 are stored in the array
this is what i got but i think there are errors
Code:
for(counter1 = 0; counter1 < ARRAY_SIZE; counter1++)
{
total = 0;
for(counter2 = index; counter2 < (WINDOW_SIZE + index); counter2++)
{
total = total + storeNumber[counter2];
}
index++;
avg = static_cast<float>(total) / WINDOW_SIZE;
cout << "The average is: " << setprecision(2) << fixed << avg << " " << index << "\n";
}
thanks