Skip to content

Instantly share code, notes, and snippets.

@samuelcouch
Created November 17, 2012 00:24
Show Gist options
  • Save samuelcouch/4092155 to your computer and use it in GitHub Desktop.
Save samuelcouch/4092155 to your computer and use it in GitHub Desktop.
Program to calculate the mean and standard deviation of a set of inputs from a file. Created for CSCI course by Sam Couch
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
//------------------------------------------------------------------------------
using namespace std;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
bool GetInputOpenFile(ifstream &inFile, string &name);
//------------------------------------------------------------------------------
bool isEmpty (string &name);
//------------------------------------------------------------------------------
double stdDev(double s[], int size);
//------------------------------------------------------------------------------
double mean(double s[], int size);
//------------------------------------------------------------------------------
int getPrecision ();
//------------------------------------------------------------------------------
//Function declarations:
//------------------------------------------------------------------------------
bool GetInputOpenFile(ifstream &inFile, string &name)
{
cout << "\n\nPlease enter the name of the input file: " << flush;
getline(cin, name);
inFile.open(name.c_str());
if ( inFile.fail() )
return false;
return true;
}
//------------------------------------------------------------------------------
double stdDev(double s[], int size)
{
double sum = 0;
double avg = mean(s, size);
for (int i = 0; i < size; i++)
{
sum += pow(s[i]-avg, 2);
}
return sqrt(sum/size);
}
//------------------------------------------------------------------------------
double mean(double s[], int size)
{
double sum = 0;
for (int i = 0; i < size; i++)
{
sum += s[i];
}
return sum / size;
}
//-----------------------------------------------------------------------------
int getPrecision()
{
int nDec;
cout << "\nEnter the desired number of decimal places for the output: " << flush;
cin >> nDec;
return nDec;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
int main()
{
ifstream inputFile;
string inputFileName;
int size = 0, nDec;
double numbers[1000], total;
cout << "In this program, we will calculate the mean and standard deviation of a set of"
<< "\nnumbers in a specified file.";
while (!GetInputOpenFile(inputFile,inputFileName))
{
cout << "\n***** ERROR *****"
<< "\n***** Could not open the input file."
<< "\n***** File name: " << inputFileName
<< "\n***** It is possible that the file does not exist or "
<< "\n***** was entered incorrectly."
<< "\n\nHit enter to try again: " << flush;
cin.get();
}
while (!inputFile.eof())
{
inputFile >> numbers[size];
size++;
}
cout << fixed << setprecision(getPrecision())
<< "\n\n Mean: " << mean(numbers, size)
<< "\nStandard Deviation: " << stdDev(numbers, size)
<< "\n\nHit enter to exit: " << flush;
cin.get();
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment