Note: You are required to do it in CPP Programming Language. Perform statistical
ID: 3856929 • Letter: N
Question
Note: You are required to do it in CPP Programming Language.
Perform statistical analysis on some data using arrays to store data for analysis by your program. Functions must be used to perform the analysis described below. All output is to be written to an output file. Description: Display Standard Header. Put your standard output information in a function named ShowHeader (). Same as previous assignments. Read and store data. Read a column of data from an external file-the file name must be entered by the user. The exact number of points is not known. There will be no more than 100 points in each data file. Find the minimum, maximum, and average. A function for each operation should be written or a function that passes these values by reference may be used (this is the preferred method). Calculate the standard deviation. The standard deviation, sigma, is the squareroot of the variance. The following formulas may be used to calculate the standard deviation. variance = 1/n sigma_i=1^n x_i^2 - 1/n^2 (sigma_i=1^n x_i)^2 or sigma = 1/n - 1 sigma_i=1^n (x_i - mu)^2 There will be three data files for you to analyze. These data files will be posted on the class web site in the near future if they have already not been posted. Do not turn in the data files! Deliverables: Programs -fully documented. All functions should be fully documented also. Output-Neatly formatted and documented. Do not turn in the input (data) files! Program design sheet-In addition to your program and the output, attach a page showing the design of your program.Explanation / Answer
program.cpp
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
void get_filename(char *, string, int);
float minimum(float *, int);
float maximum(float *, int);
float avg(float *, int);
float sd(float *, int);
int main()
{
string filename;
cout<<" Enter file name containing data : ";
cin>>filename;
//cout<<" Filename : "<<filename<<endl;
int len = filename.length();
//cout<<" Length : "<<len<<endl;
char *fname = NULL;
fname = (char *)malloc((len + 1) * sizeof(char));
if(!fname)
{
cout<<" Dynamic memory allocation failed ";
return (-1);
}
get_filename(fname, filename, len);
cout<<" File name : "<<fname<<endl;
FILE *fp = NULL;
fp = fopen(fname, "r+");
if(fp == NULL)
{
cout<<" Unable to open file ";
free(fname), fname = NULL;
return (-3);
}
int c, i = 0;
while(feof(fp) == 0)
{
fscanf(fp, "%d", &c);
cout<<c<<endl;
i++;
}
float *farr = NULL;
farr = (float *)malloc(i * sizeof(float));
if(farr == NULL)
{
cout<<" Dynamic memory allocation failed ";
free(fname), fname = NULL, fclose(fp), fp = NULL;
return (-2);
}
//cout<<endl<<i<<endl;
rewind(fp);
int k = 0;
//for(int k = 0; feof(fp) == 0; k++)
//{
while(feof(fp) == 0)
{
float f;
fscanf(fp, "%f", &f);
farr[k++] = f;
}
//cout<<endl<<k<<endl;
//for(int k1 = 0; k1 < i; k1++)
//cout<<farr[k1]<<endl;
cout<<" Minimum : "<<minimum(farr, i)<<endl;
cout<<" Maximum : "<<maximum(farr, i)<<endl;
cout<<" Average : "<<avg(farr, i)<<endl;
cout<<" Standard Deviation : "<<sd(farr, i)<<endl<<endl;
fclose(fp), fp = NULL;
free(fname), fname = NULL;
free(farr), farr = NULL;
return 0;
}
float sd(float *arr, int n)
{
float mean = avg(arr, n);
float var = 0;
for(int i = 0; i < n; i++)
{
var += pow((arr[i] - mean), 2);
}
var /= n;
return (sqrt(var));
}
float avg(float *arr, int n)
{
float sum = 0;
for(int i = 0; i < n; i++)
{
sum += arr[i];
}
return (sum / n);
}
float maximum(float *farr, int n)
{
float max = farr[0];
for(int i = 1; i < n; i++)
{
if(max < farr[i])
{
max = farr[i];
}
}
return max;
}
float minimum(float *farr, int n)
{
float min = farr[0];
for(int i = 1; i < n; i++)
{
if(farr[i] < min)
{
min = farr[i];
}
}
return min;
}
void get_filename(char *arr, string str, int len)
{
int i = 0;
for(; i < len; i++)
arr[i] = str[i];
arr[i] = '';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.