First pls view my code below...
Code:
#include <winsock.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CLASS_SIZE 60
#define subjects 5
//====================================================================================//
int main(void) {
char input[CLASS_SIZE][subjects], token[81];
int c_size;
int i, j;
float gp, total_gp = 0;
float gpa, average_gpa, total_gpa = 0;
int count = 0, countc = 0;
float min, max;
//===================================================================================//
// Ask for class size
printf("Input class size (num of students): ");
scanf("%d", &c_size);
// Looping grade question
for(i=0; i<c_size; i++)
{
for(j=0; j<subjects; j++)
{
printf("\nInput grade for student %d in subject %d: ",i+1,j+1);
scanf("%80s", &token);
/* good grade code entered */
if ((strlen(token) == 1) && (strstr("abcdfABCDF", token) != NULL))
{
/* copy the code and make it uppercase */
input[i][j] = toupper(token[0]);
}
/* bad grade code entered */
while ((strlen(token) == 1) && (strstr("abcdfABCDF", token) == NULL)) {
/* Reinput question */
printf("\nBad grade code entered.");
printf("\nGrade ranges from A to D and F only.");
printf("\n\nInput new grade for student %d in subject %d: ",i+1,j+1);
fflush(stdin);
scanf("%80s", &token);
/* copy the code and make it uppercase */
input[i][j] = toupper(token[0]);
};
}
}
//=================================================================================//
for(i=0; i<c_size; i++)
{
printf("\nStudent %d\t", i+1);
for(j=0; j<subjects; j++)
{
if(input[i][j] == 'A') {
gp = 4;
count++;
}
else if(input[i][j] == 'B') {
gp = 3;
count++;
}
else if(input[i][j] == 'C') {
gp = 2;
count++;
}
else if(input[i][j] == 'D') {
gp = 1;
count++;
}
else {
gp = 0;
count++;
}
total_gp = total_gp + gp;
printf("%c\t", input[i][j]);
}
// Calculate all students gpa
gpa = (total_gp / count);
//=================================================//
// Find max and min
max = gpa;
min = gpa;
if (gpa > max)
{
max = gpa;
i++;
}
else if (gpa < min)
{
min = gpa;
i++;
}
//==================================================//
// Calculate total of gpa
total_gpa = total_gpa + gpa;
// Print all students gpa
printf("%.1f", gpa);
// Count subject
countc++;
// Calculate average gpa
average_gpa = total_gpa / countc;
printf("\n");
// Reset count & total_gp to 0
count = 0;
total_gp = 0;
}
printf("\nAverage GPA = %.0f", average_gpa);
printf("\nHighest GPA = %.1f by Student No %d", max, i);
printf("\nLowest GPA = %.1f by Student No %d", min, i);
return 0;
}
Now please view image below...
As you can see, I face a problem which is I can't determine the highest gpa and the lowest gpa of a students. It always just display the last students gpa. So how could I solve this problem? Very thanks you.