this is weird to explain in the title alone, but i created a program below that works and compiles properly. it does everything i need it to do.
user inputs however many scores he/she wants.
6 scores get calculated for the desired calculations.
if user inputs more than 6, the other scores are irrelevant.
PROBLEM:
if user inputs LESS than 6, i get weird numbers.
i feel like my code is right, maybe im missing something really small? or possibly something extremely critical? im really not sure.
can anyone kindly look at my program below and see what could possibly be the issue? ill prolly turn it in to my teacher the way it is and lose a few points, but for my personal knowledge, id like to know what i could have possibly done wrong.
thanks you
#include <fstream>
#include <iostream>
using namespace std;
const int SIZE = 6;
// function to sort scores
int compare(const void*pa, const void* pb)
{
const int& a = *static_cast<const int*>(pa);
const int& b = *static_cast<const int*>(pb);
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
// function to average scores
double getAverage(int* score, int n)
{
int sum = 0;
int i = 0;
for (i = 0; i < n; i++)
sum += score[i];
double average = double(sum) / n;
return average;
}
// function to find A-scores
int countScoresGreater(int* score, int nScores, int b, int c)
{
int nGreater = 0;
int i;
for (i = 0; i < nScores; i++)
if (score[i] >= b && score[i] < c) nGreater++;
return nGreater;
}
// main hub for all
int main()
{
//create an empty list
const int MAX_SCORES = 6;
int nScores = 0;
int score[MAX_SCORES];
// prompt for how many students
cout << "How many records would you like to view? ";
cin >> nScores;
cin.ignore(1000, 10);
cout << " " << endl;
// read and save the scores
for (int i = 0; i < nScores; i++)
{
// read score from user
int aScore;
cout << "Enter score: ";
cin >> aScore;
cin.ignore(1000, 10);
if(i < MAX_SCORES)
score[i] = aScore;
}
qsort (score, MAX_SCORES, sizeof(int), compare);
cout << "\n Sorted: ";
int i;
for (i = 0; i < MAX_SCORES; i++)
cout << score[i] << ' ';
cout << endl;
int max = score[0];
int min = score[0];
for (i = 0; i < MAX_SCORES; i++)
{
if (max < score[i]) max = score[i];
if (min > score[i]) min = score[i];
}
cout << " " << endl;
cout << "highest score: " << max << endl;
cout << "lowest score: " << min << endl;
cout << "average score: " << getAverage(score, MAX_SCORES) << endl;
cout << "number of A scores: " << countScoresGreater(score, nScores, 90, 101) << endl;
cout << "number of B scores: " << countScoresGreater(score, nScores, 80, 90) << endl;
cout << "number of C scores: " << countScoresGreater(score, nScores, 70, 80) << endl;
cout << "number of passing scores: " << countScoresGreater(score, nScores, 70, 101) << endl;
cin >> i;
cin.ignore(1000, 10);
cin.ignore();
cin.get();
return 0;
}