Using C Write a program that accepts a text file from the co

Using C++

Write a program that accepts a text file from the command line in order to compute single
variable statistics. The program will:
a. Check if the file exists before proceeding.
b. Print the contents of the file to the screen 10 numbers per line.
c. Compute the mean and standard deviation for these values.
d. Print the mean and standard deviation to the screen.
Note: 1. The program should NOT open a file that does not exist.
2. Use a function to do each of the following separately:
- Print the file values to the screen. (10 values per line)
- Compute the mean and standard deviation and print them.
3. Test your program with the following values containted in a numbers.txt file saved in the same directory.

7
8
8
23
-3
5
8
14
14
1
2
0
11
19
15
17
12
10
8
-1
11
-3
18
5
-2
21
24
2
23
6.

Solution

//C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <stdlib.h> /* srand, rand */
#include <iomanip>
#include <limits.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>

using namespace std;

double average(vector<double> v)
{
double mean = 0;
for (int i = 0; i < v.size(); ++i)
{
mean = mean + v[i];
}

return mean/v.size();
}


double standardDeviation(vector<double> v)
{
double sd;
double mean = average(v);

for(int i = 0; i < v.size(); ++i)
sd = sd + pow(v[i] - mean, 2);

return sqrt(sd/v.size());
}

int main()
{
string filename;
cout << \"Enter filename: \";
cin >> filename;

double number;
vector<double> v;
ifstream inFile (filename.c_str());

if (inFile.is_open())
{
while (true)
{
inFile >> number;
v.push_back(number);

if(inFile.eof())
break;
}
inFile.close();
}
else cout << \"Unable to open file\";


cout << \"Contents of file: \";
for (int i = 0; i < v.size(); ++i)
{
cout << v[i] << \" \";
if(i%10 == 0)
cout << endl;
}

cout << endl;

double mean = average(v);
double sd = standardDeviation(v);

cout << \"Mean: \" << mean << endl;
cout << \"Standard Deviation: \" << sd << endl;
return 0;
}


/*
output:


Enter filename: numbers.txt
Contents of file: 7
8 8 23 -3 5 8 14 14 1 2
0 11 19 15 17 12 10 8 -1 11
-3 18 5 -2 21 24 2 23 6
Mean: 9.43333
Standard Deviation: 7.96946

*/

Using C++ Write a program that accepts a text file from the command line in order to compute single variable statistics. The program will: a. Check if the file
Using C++ Write a program that accepts a text file from the command line in order to compute single variable statistics. The program will: a. Check if the file
Using C++ Write a program that accepts a text file from the command line in order to compute single variable statistics. The program will: a. Check if the file

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site