Segmentation fault when trying to get lowest number from user inputted array

I'm making a program that calculates the average of user inputted tests, after the user has inputted the number of tests, the user then has to input every grade for the number of tests inputted, after that the program will then read into the array of grades inputted then get the lowest grade, after that the program will calculate the average, then output that, then output the lowest grade, then call function "letter();" to get the letter grade based on the value on the average. but when it goes into getting the lowest number from the array it gives me a segmentation fault that I can't fix

I have tried to redo that part of the code to no avail

#include <stdio.h>

void letter();

int average;

int main()
{
    int a[100], i, sum = 0, num, lowest, test;

    printf("Enter the numbers of tests: ");
    scanf("%d", &test);

    while (test > 100 || test <= 0)
    {
        printf("Error! The number of tests should be in range of (1 to 100).\n");
        printf("Enter the number again: ");
        scanf("%d", &test);
    }

    for(i = 0; i < test; ++i)
    {
        printf("%d. Enter grade for this test: ", i+1);
        scanf("%d", &a[i]);
        sum += a[i];
    }

    lowest = a[0];

    for (i = 0; i < num; i++)
    {
        if (a[i] < lowest)
        {
            lowest = a[i];
        }
    }

    average = sum / test;
    printf("Average = %d", average);

   return (0);
}

void letter()
{
    if (average == 0 || average <= 60)
    {
        printf("Your average is a F. \n");
    }

    else if (average <= 70 || average >= 61)
    {
        printf("Your average is a D. \n");
    }

    else if (average <= 80 || average >= 71)
    {
        printf("Your average is a C. \n");
    }

    else if (average <= 90 || average >= 81)
    {
        printf("Your average is a B. \n");
    }

    else if (average <= 100 || average >= 91)
    {
        printf("Your average is a A. \n");
    }
}

EDIT

I added

printf("Your average is: %d \n", average);

printf("The lowest grade is: %d \n", lowest);

letter();

after

average = sum / test;

and changed the letter function to this but doesnt give me correct letter grade with average

void letter()
{
    if (average >= 0 || average <= 59)
    {
        printf("Your average is a F. \n");
    }

    else if (average >= 60 || average <= 69)
    {
        printf("Your average is a D. \n");
    }

    else if (average >= 70 || average <= 79)
    {
        printf("Your average is a C. \n");
    }

    else if (average >= 80 || average <= 89)
    {
        printf("Your average is a B. \n");
    }

    else if (average >= 90 || average <= 99)
    {
        printf("Your average is a A. \n");
    }
}

EDIT #2 Changed letter function to this, now it works, thanks everyone for you help and taking your time to help me out.

void letter()
{
    if (average == 0 && average <= 60)
    {
        printf("Your average is a F. \n");
    }

    else if (average <= 70 && average >= 61)
    {
        printf("Your average is a D. \n");
    }

    else if (average <= 80 && average >= 71)
    {
        printf("Your average is a C. \n");
    }

    else if (average <= 90 && average >= 81)
    {
        printf("Your average is a B. \n");
    }

    else if (average <= 100 && average >= 91)
    {
        printf("Your average is a A. \n");
    }
}

4 answers

  • answered 2019-04-15 05:58 Sourav Ghosh

    The problem that I see is

     for (i = 0; i < num; i++)
    

    here, num is used unintialized. If you try to use the value of a variable which

    • is never initialized
    • can have trap representation
    • never has the address taken

    it invokes undefined behavior.

    Solution: You'd want to use test as the loop counter.

    And also, in your code, you never made a call to letter().

    A modified version of your code which prints the expected output.

  • answered 2019-04-15 05:59 P.W

    The variable num is not initialized and it is used in the for loop

    for (i = 0; i < num; i++)
    

    This leads to undefined behavior. The segmentation fault is probably caused by the fact that the uninitialized value of num used as an index to array a leads to an out of bounds memory access in the below check.

     if (a[i] < lowest)
    

  • answered 2019-04-15 05:59 Stephan Lechner

    At the time you enter loop

    for (i = 0; i < num; i++) {
        if (a[i] < lowest)
    

    your variable num is not initialized. This is undefined behaviour, probably leading to a segfault due to a value of i larger than 99 then.

    Write for (i = 0; i < test; i++) instead.

  • answered 2019-04-15 06:00 Sumit Trehan

    The variable num is allocated on stack and and it is not initialised so it will have garbage value. You are using it in below for loop. Hence if num is arbitrarily greater than or equal to 100, you would try to access a[num] which is array out of bound access and can result in crash. You should use test instead of num in below for loop.

    for (i = 0; i < num; i++)
        {
            if (a[i] < lowest)
            {
                lowest = a[i];
            }
        }