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.
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
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...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Your assignment is to write a C++ program to read a text file containing the information...
Your assignment is to write a C++ program to read a text file containing the information of the employees of a company, load them into memory and perform some basic human resources operations. This assignment will help you practice: multiple file programming, classes, public and private methods, dynamic memory allocation, constructors and destructors, singly linked list and files. Implementation This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of the required classes....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT