Question

Hello I need help with this simple C++ program. Write a program to calculate the average...

Hello I need help with this simple C++ program.

Write a program to calculate the average of a set of integer values placed in an input file called “data.txt”; you may create your own input file. We however do not know the number of values listed in this input file and your program should be written in a way to handle any file. Make sure to submit your input file in addition to a copy of the program and its output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    ifstream in("data.txt");
    if (in.is_open()) {
        double total = 0, count = 0, num;
        while (in >> num) {
            total += num;
            ++count;
        }
        cout << "Average of all numbers in file is " << total / count << endl;
        in.close();
    } else {
        cout << "data.txt does not exists!" << endl;
    }
    return 0;
}

7 9 1 5

Average of all numbers in file is 5.5
Add a comment
Know the answer?
Add Answer to:
Hello I need help with this simple C++ program. Write a program to calculate the average...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT