Please read a set of double data from a file named datafile
Please read a set of double data from a file named “data_file” into a one
dimensional array with size n . You may want to check if the file has enough data items. If the file has enough items to input to the array, please calculate the average value of the array items.(You should write a complete program including main) c++
Solution
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream infile(\"data_file.txt\");
int a,count=0,sum=0;
double average;
while (infile >>a)
{
count++;
sum+=a;
}
average=(double)sum/count;
cout << \"Average of \" <<average<< endl;
return 0;
}
data_file.txt
12 32 43 54 56 66 32
OUTPUT:
Average of 42.1429
