Find the average score, highest score, lowest score

So this is my assignment, Create a 1-D Array to hold a set of exam scores:
55 74 93 84 64 72 69 78 87 84 72 33 83 68 62 51 88 90

Write a program to do the following tasks:

1. Display scores in rows of four(4) scores.

2. Calculate average score and display.

3. Find Lowest score and display.

4. Find Highest score and display.

5. Find the deviation of each score, and
display the score and its deviation.

6. Find the Standard Deviation and Display

7. How many scores are within One Standard Deviation

So I have the average down, but I am getting stuck at the part of displaying the highest and lowest score. Every time I run the program I get two giant negative numbers for both of them. Was hopping someone could look at my coding and see where I went wrong. Oh and I put all the scores into a .txt file and I just opened in through the program. Thanks.

#include "TCHAR.h"
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{
const int SIZE = 18;
int numbers[SIZE];

//finding highest value
int count;
int highest;
highest = numbers [0];
for (count = 1; count < SIZE; count++)
{
if (numbers[count] > highest)
highest = numbers [count];
}
//finding lowest value

int lowest;

lowest = numbers[0];
for (count = 1; count < SIZE; count++)
{
if (numbers[count] < lowest)
lowest = numbers[count];
}
//Finding average
string dataFile = "Scores.txt";
ifstream fin;
fin.open(dataFile.c_str());

int num(0), sum(0);
double avg(0.0);


int ns=0;

fin >> num;

while (! fin.eof())
{
++ns;
sum += num;

fin >> num;
}

avg = sum/(double)ns;
cout << "Class Average: " << avg;
cout << "\n Highest Value: " << highest;
cout << "\n Lowest Value: " << lowest;
fin.close();

}
I'd say its because you're never actually comparing the exam scores in the text file but instead garbage stored in numbers when you declare it. You're going to have to open the file first, store all the numbers in the numbers array, and then find the highest, lowest, and average.
Topic archived. No new replies allowed.