I have 1000 rows in a column in a txt file. i want the average of each five row of the column. like average of 1 to 5, then average of 6 to 10, then 11 to 15 and so on.
I am using the ifstream to read the column from the txt file, but after that dnt understand how to do the average....
suppose your array is a[1000]
for(int i = 0; i<1000;)
{
int temp = a[i] + a[i+1] + a[i+2] + a[i+3] + a[i+4];
cout<<"average of "<<i+1<<"to";
i = i + 1;
cout << i << "is" << temp/5;
}
I'm sorry, HiteshVaghani1 but your code is wrong in several ways...
1 2 3 4 5 6 7 8 9 10 11
float a[1000];
for (int i = 0; i < 1000; i += 5) {
float average = 0;
for (int j = i; j < i + 5; ++j) {
average += a[j]; // get the sum of corresponding entries
}
average /= 5.f; // compute the average value
std::cout << "Average of rows " << i + 1 << " to " << i + 5 << " is " << average << std::endl;
}
I usually don't write code in answers to such questions, but I really don't want tatai to be confused. Tatai, please try to understand each line of code that I have written and ask me if something is confusing you.
Thank you KRAkatau. I could not understand,
average/=5.f;
what is 'f'.
i have the data in txt file. i need to read the data from that file first. but in that case, I cant use the ARRAY. please explain how can I read a column from a txt file as an array.