Question

Write a program that takes its input from a file of numbers of type double and...

Write a program that takes its input from a file of numbers of type double
and outputs the average of the numbers in the file to the screen. The file
contains nothing but numbers of type double separated by blanks and/or
line breaks. If this is being done as a class assignment, obtain the file name
from your instructor
C++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

INPUT FILE:

1.2 3.4 5.6 7.8
1 2 3 4
1.2

CODE

#include <iostream>

#include <fstream>

using namespace std;

int main () {

string filename;

double number, total = 0;

int count = 0;

cout << "Enter file name: ";

cin >> filename;

fstream fin(filename, fstream::in);

while (fin >> number) {

total += number;

count++;

}

cout << "Average = " << total / count;

return 0;

}

/*OUTPUT

Enter file name: numbers.txt

Average = 3.24444

*/

// ----- Up vote or comment. Happy Learning!

Add a comment
Know the answer?
Add Answer to:
Write a program that takes its input from a file of numbers of type double and...
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
  • Write a program that will take input from a file of numbers of type double and...

    Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen. Output the file name and average. Allow the user to process multiple files in one run. Part A use an array to hold the values read from the file modify your average function so that it receives an array as input parameter, averages values in an array and returns the average Part...

  • 4. Write a C++ program Write a program that takes as input five numbers and outputs...

    4. Write a C++ program Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are xi, Xa, x3, Xa, and xs, then the mean is x = (Xi + X2 + X3+ X4 + Xs)/5 and the standard deviation is: Your program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation. 30...

  • Java!!! Write a program that takes a file name as its command line argument. It assumes...

    Java!!! Write a program that takes a file name as its command line argument. It assumes the file is a binary data file and checks the first 4 bytes of the file to see whether they contain the integer −889275714. It outputs “yes” or “no” or an error message if there was an IOException generated. (Trivia question: Why test for that particular value? Hint: Try it on several .class files.)

  • Write a program that takes a file as input and checks whether or not the content...

    Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples:             [ ( ) ] { } à balanced.             [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...

  • write a program in c++ Read an infix expression from an input file and convert to...

    write a program in c++ Read an infix expression from an input file and convert to postfix. Instead of displaying directly on the screen, first place in a queue, and then display the contents of the queue on the screen. Precondition: The expression will be read from a file (input.txt) that contains a single line. There will be no spaces between the operands and the operators. The following operators are allowed: ( ) + - * / The normal rules...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • Write a program that would ask the user to enter an input file name, and an...

    Write a program that would ask the user to enter an input file name, and an output file name. Then the program reads the content of the input file, and read the data in each line as a number (double). These numbers represent the temperatures degrees in Fahrenheit for each day. The program should convert the temperature degrees to Celsius and then writes the numbers to the output file, with the number of day added to the beginning of the...

  • Write a java program that allows the user to input 20 double type numbers to an...

    Write a java program that allows the user to input 20 double type numbers to an array and then output the largest (max) number.

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Write a program that reads the scores from the file and displays their total and average.

    Suppose that a text file Lab2.txt contains an unspecified number of scores. Write a program that reads the scores from the file and displays their total and average.In the text file, the scores are separated by blanks.The program should contain two methods with these signatures (it can contain other methods besides these if you think appropriate):public static double totalScores(File inFile)public static double averageScores(File inFile)The output from the program should look like this successful sample run from my solution:The total of...

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
ADVERTISEMENT