0

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.

2
  • 1
    ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Sep 16, 2019 at 5:19
  • Fyi, you'll hit out-of-bounds of this goes to a full set of 10 grades, as grades is only index'able 0..9. Consider what for ( g = 1; g <= number_of_grades; g++ ) does when 10 is number_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. Commented Sep 16, 2019 at 5:22

1 Answer 1

2

In C, array index starts from 0 and ends at size-1.

But you are using array from 1 to size.

Probably when user gives "number_of_grades" as 10, you will be accessing array[10] which is out of range as array size is 10 and that memory may be having some garbage value.

So you can fix the issue by updating your for loops. Change

for ( g = 1; g <= number_of_grades; g++ )

to

for ( g = 0; g < number_of_grades; g++ )

everywhere.

Note:

(1) In some places array[g+1] array[g-1] are present.

You need to update the logic such that you read/write only array[k] where 0 <= k < size-1.

(2) Initial loop issue which you have mentioned in comment: You have a do-while loop inside a while loop.

while(1 ==  quit) 
{
   do{
   } while ( number_of_grades > 10)
}

I think what you are trying to do is:

  1. If user enters input more than 10, tell it is an invalid input. Give one more chance to user.
  2. If user enters 0, quit the program.

If that is the case, you can do as below.

//while(1 ==  quit)  //This is not required.
{
   do{
       scanf();
       if ( number_of_grades == 0)
       {
           //Print error
           return;
       }
       else if ( number_of_grades > 10 )
           printf ( "\n *** Invalid number of grades entered. ***\n" );
   } while ( number_of_grades > 10)
}

Ideally you should open a new post for each question. Otherwise, the post will become confusing and will not be usable to others facing similar issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.