I have a C++ program I am having trouble with... here it is.
For this programming assignment you will read a list of names and grades from a text file. The file will be user-specified. You will then print out the original list of names and grades in order by grade. You will also print the number of A's, B's, C's, D's, and F's and the average, highest, and lowest grades. It is supposed to print this out...
Joe 45
Jim 55
Sam 90
Ace 95
Bill75
Ace 95
Sam 90
Bill 75
Jim 65
Joe 45
There were 2 A's.
There were 0 B's.
There was 1 C.
There was 1 D.
There was 1 F.
Highest: 95
Lowest: 45
Average: 60.0
I am able to do everything except get the original list to print (the first block of names/grades) and I also am not sure how to do the list of letter grades.
I've done everything my professor suggested but hasn't been much help. Here is my code so far, thanks.
void Print(Str names[], int grades[], int count)
{
for (int i = 0; i < count; i++)
cout << setw(15) << left << names[i] << setw(4) << right << grades[i] << endl;
}
void SelectionSort(int grades[], Str names[], int count) // sort from highest grade to lowest grade
{
for (int i = 0; i < count - 1; i++)
{
int largest = i;
for (int j = i + 1; j < count; j++)
if (grades[j] > grades[largest])largest = j;
int tmp = grades[largest];
grades[largest] = grades[i];
grades[i] = tmp;
int GetHighest(int grades[], int count) //highest grade
{
int hi = grades[0];
for (int i = 1; i < count; i++)
if (grades[i] > hi)hi = grades[i];
return hi;
}
int GetLowest(int grades[], int count) //lowest grade
{
int low = grades[0];
for (int i = 1; i < count; i++)
if (grades[i] < low)low = grades[i];
return low;
}
float GetAverage(int grades[], int count) //average grade
{
float total = 0;
for (int i = 0; i < count; i++)
total += grades[i];
return total / count;
}
int main(void)
{
Str names[20];
int grades[20];
int count = 0;
char fn[30];
cout << "Enter file name:";
cin >> fn;
ifstream infile(fn);
if (infile)
{
for (; infile >> names[count] >> grades[count]; count++);
infile.close();
SelectionSort(grades, names, count);
Print(names, grades, count);