Question

In: Computer Science

How do I do this: Write a program that can read a text file of numbers...

How do I do this:

Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!

Solutions

Expert Solution

Hi, You have not mentioned about programming language.

Please try to provide all details.

I have implementated in C++.

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

const int MAX_SIZE = 100;

// Pre: size <= declared size of the array argument
// Post: return the standard deviation first size array elements
// Note: The divisor is the size, not size - 1 as some
// formulae call for.

double stdDev(double s[], int size);

// Pre: size <= declared size of the array argument
// Post: return the mean of the first size array elements

double average(double s[], int size);

int main()
{
// declaring an array of size 100
double s[MAX_SIZE];

// reading file name
string inputFile, outputFile;
cout<<"Enter input file name: ";
cin>>inputFile;
cout<<"Enter output file name: ";
cin>>outputFile;

// opening files
ifstream inFile(inputFile.c_str());
if(inFile.fail()){
cout<<"ERROR in opening input file"<<endl;
return -1;
}

ofstream outFile(outputFile.c_str());

int size = 0;
int num;
while(inFile>>num){
s[size] = num;
size = size + 1;
}

// calculating mean
double mean = average(s, size);
cout<<"Mean : "<<mean<<endl;

// calculating standard deviation
double stdDeviation = stdDev(s, size);
cout << "The Standard Deviation is: "<< stdDeviation << endl;

// writing in file
outFile<<"Mean: "<<mean<<endl;
outFile<<"Standard deviation: "<<stdDeviation<<endl;

// closing files
inFile.close();
outFile.close();

return 0;
}

//Write the function header here…
double stdDev(double s[], int size)
{
double sumSquares = 0;
double avg = average(s, size);

//Write the for loop here…
for(int i=0; i<size; i++){
sumSquares += pow(s[i]-avg, 2);
}

return sqrt(sumSquares / size);
}

//Write the function header here…
double average(double s[], int size)
{
double sum = 0;

//Write the for loop here…

for(int i=0; i<size; i++)
sum += s[i];

return sum / size;
}


Related Solutions

Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Write a program that processes numbers, corresponding to student records read in from a file, and...
Write a program that processes numbers, corresponding to student records read in from a file, and writes the required results to an output file (see main ( )). Your program should define the following functions: double read_double (FILE *infile) — Reads one double precision number from the input file. Note: You may assume that the file only contains real numbers. int read_integer (FILE *infile) - Reads one integer number from the input file. double calculate_sum (double number1, double number2, double...
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT