### Calculating mean/std dev from an array filled from a file

Hello all, I am working on a program for a class and so far I've had little issues, but I am unable to get in touch with my professor today and was wondering if you all could take a look at this for me?

The program obviously isn't complete, but right now I have a function that will read in numbers from a txt file and fill an array with those values as long as they are 0-100. The issue is, it seems that the function is still filling the array with invalid values. Right now I am only using 5 grades as to make it easier to test. Also, I am displaying the grades in a first come order for now, but I will tackle that later.
//------------------------------------------------------------------------------
// Zerrigan
// CIS155-01
// Assignment 10
// This program reads up to 30 values from the file data10.txt as input, puts
// them in an array called grades if the value is between 0 - 100 inclusive,
// calculates the mean and standard deviation of the accepted values, and
// displays the accepted values in reverse order of being read, along with
// displaying the mean and standard deviation of the accepted values.
//------------------------------------------------------------------------------

#include <iostream>
#include <fstream>
using namespace std;

void fillArray (int a[], int size, int& numberUsed);
// While size is the declared size of the array a, numberUsed will be the
// number of values stored in a as a[0] through a[numberUsed-1] are filled with
// integers read from data10.txt

void average (int a[], int size, int& numberUsed);
// Calculates the mean of the values entered into the array a.

void standardDeviation (int [a], int size, int& numberUsed);
// Calculates the standard deviation of the values entered into the array a.

const int NUMBER_GRADES = 5;

fstream inputStream;

int main()
{
cout << "This program reads values from the file data10.txt and" << endl
<< "places each value in an array called grades if the value ranges "
<< "from 0 to 100." << endl << "The program also calculates the mean "
<< "and standard deviation" << endl << "of all of the accepted values."
<< endl << endl;

int grades[NUMBER_GRADES],numberUsed;

inputStream.open("data10.txt");

fillArray(grades, NUMBER_GRADES, numberUsed);

//sort(grades, numberUsed);

cout << "The grades in order are:" << endl;
for (int index = 0; index < numberUsed; index = index + 1)
cout << grades[index] << endl;

system("pause");
return 0;
}

//------------------------------------------------------------------------------

void fillArray (int a[], int size, int& numberUsed)
{
int score, index; // score is the value being read from the file

inputStream >> score;

for (index = 0; index < NUMBER_GRADES; index = index + 1)
{
if ((score < 0) || (score > 100))
inputStream >> score;

else
{
a[index] = score;

inputStream >> score;
}
}
numberUsed = index;
}
Last edited on
Topic archived. No new replies allowed.