I need a write a program that reads from a file (a dat file) this numbers (for e
ID: 3625527 • Letter: I
Question
I need a write a program that reads from a file (a dat file) this numbers (for example)
5
5 7 3 6 6
2
2 5
and calculates the standard deviation, mean, variance, sum, range. It should display like this format and write to another txt file.
|# of data| sum | range | mean | variance | standard deviation|
_____________________________________________________________________
| 5| 27| 3 to 7| 5.400| 2.300| 1.517 |
_____________________________________________________________________
| 7| 34| 2 to 7| 4.857| 3.143| 1.773 |
I need a program like this for statistics written in c language. I plan to take c/C++ but not for a while. Any help appreciated.
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
//Function prototype declaration
double standard_Deviation(double x[],int n);
//Main function
void main()
{
ifstream inputFile;
ofstream outFile;
//variable declaration
double x[100];
double mean,sdeviation;
int i,n;
inputFile.open("Number.txt");
//Inputting array values
cout<<"Reading values from file:" <<endl;
inputFile>>num;
while(num>0)
{
x[i]=num;
i++;
inputFile>>num;
}
n=i;
//function call standard deviation
standard_Deviation(x,n);
//Outputting standard deviation
cout<<"Standard deviation:"<<sdeviation<<endl;
//pause system for a while
system("pause");
}
void standard_Deviation(double x[],int n)
{
ofstream outFile;
outFile.open("Output.txt");
double sd=0;
double mean,sum=0;
for(int i=0;i<n;i++)
sum=sum+x[i];
mean=sum/n;
for(int i=0;i<n;i++)
sd=sd+pow((x[i]-mean),2);
sd=sd/n;
outFile<<" sum | range | mean | variance | standard deviation|"<<endl;
outFile<<sum<<" "<<mean<<" "<<sd <<" "<<sqrt(sd)<<" ";
}//End standard_Deviation
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.