Calculate average from file array

Hello,

I am working on an assignment where I have a file with 300+ "test scores". However, I have to set a size of 500 values. None of the numbers are negative.

The assignment requires 4 functions:

Input from File
I have already done this. Just had to build an array from the data file.

Calculate Average
I have to calculate the average of these scores and then return the average to the main function, where it will be displayed on screen. This is where I am having the problem right now. I add them up and divide, but I'm not getting the correct count for the division. I believe that it is dividing by 0.

Calculate max/min value
Haven't gotten to this yet.

Calculate standard diviation.
Haven't gotten to this yet.

Determine and write quartile summary
Haven't gotten to this yet.

I try to write the functions separately to make sure they work, and then bring them together.

I appreciate any help I can get!

Here is my code:

#include <iostream>
#include <fstream>

using namespace std;

const int SIZE = 500; //Size declaration of array
int testScores[SIZE]; //Array declaration
int scores;
void inputFromFile(int [], int );
double CalculateAverage(int[], int);

main()
{
int average;
inputFromFile(testScores, SIZE);


CalculateAverage(testScores, SIZE);
cout << "The average of the test scores is: " << average << endl;

system("pause");
return 0;
}

void inputFromFile(int values[], int size)
{
int count = 0; //Loop counter variable
ifstream inputFile; //Input file stream object

inputFile.open("i:\\Winter 2011\\CST 180 - C++\\array_pgmdata.txt");
if (!inputFile) cout << "File did not open";

while (count < SIZE && inputFile >> testScores[count])
count++;

inputFile.close();

cout << "The numbers are: ";
for (int index = 0; index < count; index++)
cout << testScores[index] << " ";
cout << endl;
}

double CalculateAverage(int[], int)
{
int count;
double total = 0;
double average;

for (int count = 0; count < count; count++)
cout << testScores[count] << " ";
cout << endl;

cout << "In CalculateAverage function. count = " << count << endl;
average = total/count;
return average;
}
1. main() needs to return an int
2. Please use [code][/code] tags around your code
3. Your CalculateAverage() function doesn't seem to actually sum a total anywhere, so you always end up with 0/count
Ok, sorry, I thought I had the addition statement in the function. I've added it now, but it still isn't working correctly.

I'm not sure where I don't have brackets around my code. I thought that was covered.

Here is my revised code:

#include <iostream>
#include <fstream>

using namespace std;

const int SIZE = 500; //Size declaration of array
int testScores[SIZE]; //Array declaration
int scores;
void inputFromFile(int [], int );
int CalculateAverage(int[], int);

main()
{
int average;
inputFromFile(testScores, SIZE);


average = CalculateAverage(testScores, SIZE);
cout << "Back in main\n";
cout << "The average of the test scores is: " << average << endl;

system("pause");
return 0;
}

void inputFromFile(int values[], int size)
{
int count = 0; //Loop counter variable
ifstream inputFile; //Input file stream object

inputFile.open("i:\\Winter 2011\\CST 180 - C++\\array_pgmdata.txt");
if (!inputFile) cout << "File did not open";

while (count < SIZE && inputFile >> testScores[count])
count++;

inputFile.close();

cout << "The numbers are: ";
for (int index = 0; index < count; index++)
cout << testScores[index] << " ";
cout << endl;
}

int CalculateAverage(int[], int)
{
int count;
double total;
double average;

for (int count = 0; count < count; count++)
{
cout << testScores[count] << " ";
total =+ testScores[count];
}
cout << "\nIn calculateAverage function: total = " << total << endl;
cout << endl;
average = total/count;
return average;
}
shouldn't double total be initialized to zero?
I tried that - when I did, my test "out << "\nIn calculateAverage function: total = " << total << endl;" prints out total = 0.
1. main() still needs to return an int
2. You still aren't using tags around your code
3. The correct operator is +=, not =+. =+ does not do what you want.
Topic archived. No new replies allowed.