Question

In: Computer Science

In C++, write a program that reads data from a text file. Include in this program...

In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES OF THE PROGRAM MUST BE COMMENTED.

349.5

376.2

36.9

283.5

361.0

381.0

344.7

328.5

368.3

370.5

360.6

426.9

434.7

Help please and comment on every line.

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// dataFile999.txt

349.5
376.2
36.9
283.5
361.0
381.0
344.7
328.5
368.3
370.5
360.6
426.9
434.7

________________

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

int cnt;
double mean,stdDev;

//Function declarations.
void calMean(double vals[]);
void calStdDev(double vals[]);
int main()

{
   //setting the precision to two decimal places
   std::cout << std::setprecision(2) << std::fixed;
     
   //Declaring variables
ifstream dataIn;
double val;
  
//Opening the input file
dataIn.open("dataFile999.txt");
if(dataIn.fail())
{
   cout<<"** File Not Found **"<<endl;
   return 1;
       }
       else
       {
           while(dataIn>>val)
           {
           cnt++;  
           }
          
           dataIn.close();
          
// Creating array dynamically
double* vals = new double[cnt];
  
//Opening the input file
dataIn.open("dataFile999.txt");
  
           /* Reading the data from the file and
           * populate the values in the array
           */
       for(int i=0;i<cnt;i++)
       {
           dataIn>>val;
           vals[i]=val;
           }
           //calling the functions
           calMean(vals);
           calStdDev(vals);
          
           cout<<"Mean :"<<mean<<endl;
           cout<<"Standard Deviation :"<<stdDev<<endl;
       }

return 0;

}
void calMean(double vals[])
{
   double sum=0;
   for(int i=0;i<cnt;i++)
   {
       sum+=vals[i];
   }
mean=sum/cnt;
}
void calStdDev(double vals[])
{
   //Declaring local variables
double standard_deviation = 0.0, variance = 0.0, sum_of_squares = 0.0;
/* This loop Calculating the sum of
* square of eeach element in the array
*/
for (int i = 0; i < cnt; i++) {
/* Calculating the sum of square of
* each element in the array
*/
sum_of_squares += pow((vals[i] - mean), 2);
}
//calculating the variance of an array
variance = ((double) sum_of_squares / (cnt - 1));
//calculating the standard deviation of an array
stdDev = sqrt(variance);
}
_____________________

Output:

_______________Could you plz rate me well.Thank You


Related Solutions

Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER IS PREFERRED THANK YOU IN ADVANCE...
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT