Directory:Derek Elder/Programs/Deviation Calculator

MyWikiBiz, Author Your Legacy — Sunday December 22, 2024
Jump to navigationJump to search
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;

void greeting(void);
void getInFile(ifstream&, string&);
double getMean(int[], int);
double getStandardDeviation(int[], int, double);
int getGreatest(int[], int);
int getLeast(int[], int);

void main()
{
	ifstream Input;
	string Input_file;
	int statisticsData[100] = {};
	int arrayIndex = 0;
	double mean = 0.0;
	double standardDeviation = 0.0;
	int greatest = 0;
	int least = 0;
	int numberOfValues = 0;

	greeting();

	getInFile(Input, Input_file);
	cout<<"Reading values . . .\n";
	while(Input)
	{
		Input>>statisticsData[arrayIndex];
		if(!Input)
		{
			Input.clear();
			break;
		}
		arrayIndex++;
	}

	Input.close();

	numberOfValues = arrayIndex;
	mean = getMean(statisticsData, numberOfValues);
	standardDeviation = getStandardDeviation(statisticsData, numberOfValues, mean);
	greatest = getGreatest(statisticsData, numberOfValues);
	least = getLeast(statisticsData, numberOfValues);
	cout<<"Number of values read : "<<numberOfValues<<endl;
	cout<<"Mean of the values    : "<<mean<<endl;
	cout<<"Standard deviation    : "<<standardDeviation<<endl;
	cout<<"Greatest value        : "<<greatest<<endl;
	cout<<"Least value           : "<<least<<endl;
	cout<<"End of the program -- Press ENTER"<<endl;
}

void greeting()
{
	cout<<"This program will produce statistics for at most 100 real values.\n";
}

void getInFile(ifstream& Input, string& filename)
{
	bool done = false;
	char peek;

	while(!done)
	{
		Input.clear();
		cout<<"Enter the name of the data file: ";
		getline(cin,filename);
		Input.open(filename.c_str());

		if(!Input)
		{
			cout<< "\nInput file location was invalid.\n";
			done = false;
		}
		else
		{
			peek = Input.peek();
			if(peek == EOF)
			{
				cout<<"\nInput file was empty.\n";
				done = false;
				Input.close();
			}
			else
			{
				done = true;
			}
		}
	}
}

double getMean(int statisticsData[], int numberOfValues)
{
	int iterations = 0;
	double statSum = 0.0;
	int nextStat = 0;
	double mean = 0.0;
	for(iterations = 0; iterations < numberOfValues; iterations++)
	{
		nextStat = statisticsData[iterations];
		statSum = statSum + nextStat;
	}

	mean = statSum/numberOfValues;
	return mean;
}

double getStandardDeviation(int statisticsData[], int numberOfValues, double mean)
{
	double standardDeviation = 0.0;
	int iterations = 0;
	double statSquaresSum = 0.0;
	int nextStat = 0;

	for(iterations = 0; iterations < numberOfValues; iterations++)
	{
		nextStat = statisticsData[iterations];
		nextStat = nextStat * nextStat;
		statSquaresSum = statSquaresSum + nextStat;
	}

	standardDeviation = sqrt(((statSquaresSum)/(numberOfValues))-(mean*mean));
	return standardDeviation;
}

int getGreatest(int statisticsData[], int numberOfValues)
{
	int iterations = 0;
	int firstStat = 0;
	int secondStat = 0;
	int greatest = 0;

	for(iterations = 0; iterations < numberOfValues; iterations++)
	{
		firstStat = statisticsData[iterations];
		secondStat = statisticsData[iterations+1];
		if(firstStat > secondStat)
		{
			greatest = firstStat;
		}
	}

	return greatest;
}

int getLeast(int statisticsData[], int numberOfValues)
{
	int temp = 0;
	int least = statisticsData[0];

	for(int iterations = 0;iterations < numberOfValues; iterations++)
	{
		temp = statisticsData[iterations];
		if(temp < least)
		{
			least = temp;
		}
	}

	return least;
}