Question: Looking for help to code this in C++: Overview This project deals with basic statistics. You will finish coding a program that will read in
Looking for help to code this in C++:
"Overview
This project deals with basic statistics. You will finish coding a program that will read in statistical trials of 100 elements each. All trials have exactly 100 elements, but there could be any number of trials.
Specifications
The finished program should be able to:
1) Read in each trial into a hundred element array
2) Calculate the mean for that trial
3) Determine the median for that trial
4) Determine the minimum value for that trial
5) Determine the maximum value for that trial
6) Calculate the standard deviation for that trial
7) Print out the results of each trial to an output file
A basic template is given as a starting point, but the rest of the program is your responsibility. The following function at a minimum should be present: findMean calculate the mean of an array of numbers findMedian determine the median of an array of numbers findMinimum determine the minimum value in an array of numbers findMaximum determine the maximum value in an array of numbers findStdDev calculate the standard deviation of an array of numbers sortArray sort an array of numbers from smallest to largest printTrialResults print the results for each trial
Requirements
1) Trial number: column width 11, left justified
2) Mean: column width 12, left justified, fixed 1 decimal point
3) Median: column width 14, left justified, fixed 1 decimal point
4) Minimum: column width 11, left justified, fixed 1 decimal point
5) Maximum: column width 11, left justified, fixed 1 decimal point
6) Standard Deviation: column width 15, left justified, fixed 1 decimal point "
Here is what I currently have:
"
#include#include #include #include using namespace std; /// Declare global constants const char* IN_FILE_NAME = "stats.txt"; const char* OUT_FILE_NAME = "results.txt"; const int ELEMENTS = 100; /// Function Prototypes bool getTrialElements(ifstream&, double[], int); void sortElements(double[], int); double findMean(double[], int); double findMedian(double[], int); double findMinimum(double[], int); double findMaximum(double[], int); double findStdDev(double[], int); void printTrialResults(ofstream&, int, double[], int); /// This program performs basic statistics on a large set of data points int main() { /// Declare variables ifstream inFile; ofstream outFile; int trialNumber = 0; double elementArray[ELEMENTS]; /// Open input and output files inFile.open(IN_FILE_NAME); outFile.open(OUT_FILE_NAME); /// Loop through all of the data sets in the input file while(getTrialElements(inFile, elementArray, ELEMENTS)) { /// Keep track of the number of data sets processed trialNumber++; /// Output the results to the output file printTrialResults(outFile, trialNumber, elementArray, ELEMENTS); } /// Close input and output files outFile.close(); inFile.close(); return 0; } /// Function definitions /// Reads in trial elements into array if available bool getTrialElements(ifstream& inFile, double elementArray[], int arraySize) { /// Declare variable bool isData = false; /// Is there any more data to read if(!inFile.eof()) { /// Read the elements into the array for(int index = 0; index < arraySize; index++) { inFile >> elementArray[index]; } /// There is data isData = true; } // cout << "Is there data: " << isData << endl; return isData; } void sortElements(double elementArray[], int arraySize) { /// Define local variable double temp; /// Define outer loop for(int outer = 0; outer < arraySize - 1; outer++) { /// Define inner loop for(int inner = outer + 1; inner < arraySize; inner++) { if(elementArray[inner] < elementArray[outer]) { temp = elementArray[inner]; elementArray[inner] = elementArray[outer]; elementArray[outer] = temp; } } } return; } double findMean(double elementArray[], int arraySize) { double sum = 0; for (int index = 0; index < arraySize; index++) { sum += elementArray[index]; } return sum/arraySize; } double findMedian(double elementArray[], int arraySize) { double medianResult = 0; sortElements(elementArray, arraySize); if(arraySize % 2 == 0) { medianResult = (elementArray[(arraySize/2)-1] + elementArray[arraySize/2])/2; } else { medianResult = elementArray[(arraySize - 1) / 2]; } return medianResult; } double findMinimum(double elementArray[], int arraySize) { sortElements(elementArray, arraySize); return elementArray[0]; } double findMaximum(double elementArray[], int arraySize) { sortElements(elementArray, arraySize); return elementArray[arraySize - 1]; } double findStdDev(double elementArray[], int arraySize) { double stdDevFinal = 0; double meanSqdDif = 0; double variance = 0; double mean = 0; int index; mean = findMean(elementArray, arraySize); for (index = 0; index < arraySize; index++) { variance = pow(elementArray[index] - mean, 2) + variance; } meanSqdDif = variance/arraySize; stdDevFinal = sqrt(meanSqdDif); return stdDevFinal; } void printTrialResults(ofstream& outFile, int trialNumber, double elementArray[], int arraySize) { /// Output the results cout << trialNumber << " mean: " << findMean(elementArray, arraySize) << " median: " << findMedian(elementArray, arraySize) << " min: " << findMinimum(elementArray, arraySize) << " max: " << findMaximum(elementArray, arraySize) << " std dev: " << findStdDev(elementArray, arraySize) << endl; return; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
