A Class of 40 students has received their grades for 5 exams (each out of 100). Write a structured C++ program (using top-down design) that will do the following:
a. Write function readData which reads the student data list which consist of the Student ID
b. Write function printAvg that prints the ID and average score for each student for 5 consecutive grades. Do the necessary validation on input.
c. Write a function worstAvg that calculates the worst average grade.
d. Write a function that prints the IDs of all students having the worst average grade
I wrote the code and it runs, however it calculates the average grade of each student wrong and I can't figure out why. Is it the for loop? Could someone please have a look at the code and tell me what I'm missing.
Thanks in advance!
// Program directives
#include <iostream>
#include <string>
usingnamespace std;
// Supporting Functions
void readData (int);
void printAvg (int, int [], float []);
void worstAvg (int, float []);
void printID (int, int [], float [], float);
// Global variables
int numStudents; // Number of students input
int sizeID = 40;
int ID[40]; // ID Array
longint sizeGrades = 200;
float Grades[200]; // Grades Array
float Avg[40];
int id; // ID input
float scores, total=0; // Grades input
float worst = 100; // Worst possible Average
// Defining main function
int main()
{
// Input
cout << "Enter the number of students: ";
cin >> numStudents;
while (numStudents > 40)
{
cout << "Number of students is more than 40! Re-enter: ";
cin >> numStudents;
}
readData(numStudents);
return 0;
} // End of main function
// End of readData function
void readData(int numStudents)
{
// Entering students ID
for (int k = 0; k < numStudents; k++)
{
cout << "Enter the Student ID: ";
cin >> id;
ID[k] = id;
}
// Entering the 5 scores of the students
for (int i = 0; i < numStudents; i++)
{
cout << "Enter the 5 scores of the Student: " << endl;
for (int j = 0; j < 5; j++)
{
cin >> scores;
while (scores > 100)
{
cout << "The score entered is bigger than 100! Re-enter: ";
cin >> scores;
}
Grades[i] = scores;
}
}
printAvg (numStudents, ID, Grades);
} // End of readData function
// Defining printAvg function
void printAvg(int numStudents, int ID[], float Grades[])
{
float sum = 0;
for (int n = 0; n < numStudents; n++)
for (int m = 0; m <= (numStudents * 5); (m = m + 5))
{
sum = sum + (Grades[m] + Grades[m+1] + Grades[m+2] + Grades[m+3] + Grades[m+4]);
Avg[n] = (sum / 5);
}
for (int c = 0; c < numStudents; c++)
cout << "ID: " << ID[c] << " Average: " << Avg[c] << endl;
worstAvg(numStudents, Avg);
} // End of printAvg function
// Defining worstAvg function
void worstAvg(int numStudents, float Avg[])
{
for (int s = 0; s < numStudents; s++)
if (Avg[s] < worst)
worst = Avg[s];
cout << "The worst average of them all is: " << worst << endl;
printID(numStudents, ID, Avg, worst);
} // End of worstAvg function
// Defining printID function
void printID(int numStudents, int ID[], float Avg[], float worst)
{
for (int h = 0; h < numStudents; h++)
if (Avg[h] == worst)
cout << "ID: " << ID[h] << " Average: " << Avg[h] << endl;
} // End of printID function
You write all 5 scores for student i in the ith element of the array. However, elements 0 - 4 should belong to student 0, 5 - 9 to student 1, etc.
When you post code, pleas use code tags. This will make it easier for us to read and comment on. Type "[code ]" (without the spaces) before and "[/code ]" after your code. You can also edit your post, highlight your code and click the "<>" Format button.
This did work once and generated a random board, but on the second run of the
program the board remains the same. I am new to C++ and trying to understand
why it will no generate a new set of random numbers into the board 1-9.
You need to seed the random number generator. Otherwise, random_shuffle will use the same sequence of random numbers each time, leading to the same resultant order of your array.
std::srand ( unsigned ( std::time(0) ) );
Do this once at the beginning our your program. See the example here: