I'm creating a grading calculator that seems to function correctly. However, when the array sorts, it outputs a "0" as one of my array values when there isn't a "0" in the input. I'm also having trouble setting up a flag (not an exit statement) to end the program if the user wants to calculate 0 grades.
I have tried changing initialized values, changing values assigned in the bubble sort, and run the program multiple times with various inputs. I'm severely new to programming in general and the answer is probably staring me in the face but I can't seem to fix it.
#include <stdio.h>
int main (void)
{
/* Declare variables */
/* ----------------- */
int number_of_grades;
int quit;
int g;
int grades[10] = {0};
int grade_total = 0;
int temp;
int highest_grade;
int lowest_grade;
float average_grade;
char changed = 'T';
char c;
/* Output initial greeting and prompt user for the number of grades to be entered, making sure the user enters 1-10 grades. If they enter a 0, exit the program. If they enter a number above 10, reprompt. */
/* -------------------------------------------------------------------------------------------------------------------------------- */
printf ( "Welcome to the Grade Calculator\n" );
quit = 1;
while (quit == 1)
do
{
printf ( "\nEnter the number of grades to be processed (0 - 10): " );
scanf ( "%i", &number_of_grades );
while ( (c = getchar() != '\n') && c != EOF );
if ( number_of_grades == 0)
quit = 0;
else if ( number_of_grades > 10 )
printf ( "\n *** Invalid number of grades entered. ***\n" );
} while ( number_of_grades > 10 );
/* Prompt user to enter the grades. Ensure the grade is between 0-100. If not, reprompt. */
/* ------------------------------------------------------------------------------------- */
for ( g = 1; g <= number_of_grades; g++ )
{
do
{
printf ( "\nEnter grade for student #%i: ", g );
scanf ( "%i", &grades [g] );
while ( (c = getchar() != '\n') && c != EOF );
if ( grades [g] < 0 || grades [g] > 100 )
printf ( "\n***Invalid Entry. Grade must be 0 to 100.***\n" );
} while ( grades [g] < 0 || grades [g] > 100 );
grade_total = grade_total + grades[g];
}
/* Calculate the average grade */
/* --------------------------- */
average_grade = (float) grade_total / number_of_grades;
/* Sort the array from lowest to highest grade. */
/* -------------------------------------------- */
while ( changed == 'T' )
{
changed = 'F';
for ( g = 1; g < number_of_grades; g++ )
{
if ( grades [g] > grades [g + 1])
{
temp = grades [g];
grades [g] = grades [g + 1];
grades [g + 1] = temp;
changed = 'T';
}
}
}
/* Calculate the lowest and highest grades input by the user */
/* --------------------------------------------------------- */
lowest_grade = grades[1];
highest_grade = grades [g-1];
/* Output the lowest grade, the highest grade, the average grade, and the grades sorted in ascending order to the user. */
/* -------------------------------------------------------------------------------------------------------------------- */
printf ( "\nThe minimum grade is %i", lowest_grade );
printf ( "\nThe maximum grade is %i", highest_grade );
printf ( "\nThe class average is %.1f\n", average_grade );
printf ( "\nThe %i grades entered are:\n\n", number_of_grades );
for ( g = 0; g < number_of_grades; g++ )
printf ( "%i ", grades [g] );
printf ( "\n" );
printf ( "\nThank you for using the Grade Calculator\n" );
getchar();
} /* End main */
Re: Sorted Array: When I input three grades (for example: 100, 98, 92), it outputs a sorted array of "0, 92, 98". Re: Quit Statement for input of "0" grades to calculate: When the program prompts for number of grades and a "0" is entered, output still includes final min, max, avg, and sorted array statement instead of just final greeting.
gradesis only index'able0..9. Consider whatfor ( g = 1; g <= number_of_grades; g++ )does when10isnumber_of_grades. Related, think about this problem hard, because in reality you need no array storage, nor sorting, whatsoever, to determine the single value minimum, maximum, and average scores.