0
Help about SMA
i have some datas in data.txt ex:1,3,5,7,8,18,4,1,4,3,5,7... and i need a variable, (cout<<"enter a number"; cin>>n; ex:3 -- n=3 Result[0]={1} //(1)/1 Result[1]={2} // (1+3)/2 Result[2]={3} // (1+3+5)/n Result[3]={5} // (3+5+7)/n Result[4]={6.6} // (5+7+8)/n Result[5]={11} // (7+8+18)/n Result[6]={10} // (8+18+4)/n until my datas ends. -----ex:5 -- n=5 Result[0]={1} //(1)/1 Result[1]={2} // (1+3)/2 Result[2]={3} // (1+3+5)/3 Result[3]={4} // (1+3+5+7)/4 Result[4]={4,8} // (1+3+5+7+8)/n Result[5]={8,2} // (3+5+7+8+18)/n Result[6]={18,4} // (5+7+8+18+4)/n until my datas ends. https://code.sololearn.com/cM19L14d5ol3/?ref=app
4 Réponses
+ 1
You're almost there! You just need to add another nested for loop for result that is less than n.
You can do
for(int i = 0, sum = 0 ; i < l_size - 1 ; i++)
{
cout << "Numbers : ";
sum += N[i];
for(int j = 0 ; j <= i ; ++j)
{
cout << N[j] << " ";
}
mAvg = sum / (i+1);
cout << "\nMoving Average: " << mAvg << "\n\n";
}
+ 1
I mean you need to add this to your code. Not replace it.
+ 1
Thank you, thank you , thank you :)
0
CarrieForle thank you but it is stopping at n. For ex. n is 3
Numbers : 1
Moving Average: 1
Numbers : 1 3
Moving Average: 2
No more
--
Numbers : 1
Moving Average: 1
Numbers : 1 3
Moving Average: 2
Numbers : 1 3 5
Moving Average: 3
Numbers :3 5 7
Moving Average: 5
Numbers :5 7 8
Moving Average: 6.66
.......
.......
.......
.......
https://code.sololearn.com/cqe1ytM1Sicl/?ref=app