Question

In: Computer Science

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

Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the 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.

File name: pr01hw05input.txt

78.0 87.5 98.1 101.0 4.3 17.2
78.0 14.5 29.6 10.2 14.2 60.7
78.3 89.3 29.1 102.3 54.1 67.2
78.6 67.5 92.1 80.0 64.3 37.3
88.0 80.5 49.2 100.0 64.9 67.4
74.5 83.4 99.1 90.4 54.3 67.1
78.1 27.5 19.1 100.5 64.3 67.8
73.7 87.9 94.5 110.1 54.2 57.5
78.0 82.5 89.1 150.7 8.32 69.6

Solutions

Expert Solution

If you have any doubts, please give me comment..

#include<iostream>

#include<fstream>

using namespace std;

int main(){

    string filename;

    cout<<"Enter input filename: ";

    cin>>filename;

    ifstream inFile;

    inFile.open(filename.c_str());

    if(inFile.fail()){

        cout<<"Unable to open file"<<endl;

        return -1;

    }

    double num, sum = 0;

    int n = 0;

    while(inFile>>num){

        sum += num;

        n++;

    }

    double avg = sum / n;

    cout<<endl<<"Average is: "<<avg<<endl;

    return 0;

}


Related Solutions

Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
Write a program that takes a date as input and outputs the date's season. The input...
Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June 20 Summer:...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a program in python that takes a date as input and outputs the date's season....
Write a program in python that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a program that takes, as input, five numbers and outputs the mean (average) and standard...
Write a program that takes, as input, five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are x₁, x₂, x₃, x₄, and x₅, then the mean is: x = (x₁+ x₂+ x₃+ x₄+x₅)/5 and the standard deviation is: s = √(((x₁-x)²+(x₂-x)²+(x₃-x)²+(x₄-x)²+(x₅-x)²)/5) 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. Format your output with setprecision(2) to ensure...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT