0
How to write a C++ program that retrieves all the numbers stored in a data file named Project ?
As each number is retrieved, it is displayed on the screen. When the end of file is reached, the average of the numbers is displayed accurate to the nearest tenth. NOTE: Create a data file named Project and then copy and type the following numbers one 7 4 12 3 EXAMPLE: Given the numbers above, the output should be The average of 7 4 12 3 is 6.5
1 Antwort
0
float average(int sum, int length){
return sum/length;
}
int main(){
int length, sum = 0;
int x;
string filepath = "/PathTo/Yourfile/name.txt";
ifstream infile(filepath);
while(infile >> x){
length++;
sum += x;
}
cout << "Average of 7 4 12 3 is: " << average(sum, length) << endl;
return 0;
}