write simple program in c in which source reads a file of in

write simple program in c++ in which source reads a file of integers and outputs the average

Solution

Average.cpp

#include <iostream>
#include<fstream>
using namespace std;

int main()
{
ifstream inputFile;
inputFile.open(\"numbers.txt\");
int n;
int sum =0;
int count = 0;
double average = 0;
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile >> n;
sum = sum + n;
count++;
}
}
average = sum/(double)count;
cout<<\"The average is \"<<average<<endl;
inputFile.close();
return 0;
}

Output:

The average is 5.5   

Input file: numbers.txt

1 2 3 4 5 6 7 8 9 10

write simple program in c++ in which source reads a file of integers and outputs the averageSolutionAverage.cpp #include <iostream> #include<fstream>

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site