Search code examples
cmaxmin

The minimum value is not recognize


The maximum value is recognize but not in minimum value ? Can you help me find the problem in this code. Or there is problem on compiler?

#include<stdio.h>

int main()
{
    int e,i,sval,lval;

    printf("\nInput the length of the array:");
    scanf("%d", &e);

    int v[e];
    printf("\nInput the array element:\n");

    for(i=1; i<=e; i++)
    {
        scanf("%d", &v[i]);
    }
    for(i=1; i<=e; i++)
    {
        if(sval > v[i])
        {
            sval = v[i];
        }
        else
        {
            lval = v[i];
        }

    }

    printf("Smallest value : %d\n", sval);
    printf("Larger value : %d\n", lval);

    return 0;
}

Solution

  • There were a few problems with this code:

    1. Arrays in C, start at index 0, so we needed to adjust in a few places.
    2. The min and max values needed to be initialized appropriately.
    3. There was a logic issue when determining which value was the smallest/largest during your loop.

    Please see comments inline.

    #include <limits.h>
    #include <stdio.h>
    
    int main()
    {
        int e, i, min_val, max_val;
        min_val = INT_MAX;
        max_val = INT_MIN;
        // Need to initialize these to min/max values
        // We set the smallest value to the largest value because
        // presumably all other values will be lower than this. 
        // The same is true for the larger value.
    
        printf("\nInput the length of the array:");
        scanf("%d", &e);
    
        int v[e];
        printf("\nInput the array element:\n");
    
        // as mentioned, arrays start at 0
        for(i = 0; i < e; i++)
        {
            scanf("%d", &v[i]);
        }
        for(i = 0; i < e; i++)
        {
            if (min_val > v[i]) {
                min_val = v[i];
            }
            // Need a separate condition here, not an else.
            // In your example, you had the statement:
            // "If the value I am currently looking at is smaller than
            // my currently known smallest value, assign it to the larger
            // value" <- this is a logic flaw. It becomes apparent in sets
            // such as the one shown: [9, 8, 2, 6]
            // In your example, when we reach the iteration
            // where we are looking at the 6, since that is larger
            // than the currently known smallest value of 2, we would 
            // assign the 6 to the largest value, when it should be 9.
            if (max_val < v[i]) {
                max_val = v[i];
            }
        }
    
        printf("Smallest value : %d\n", min_val);
        printf("Larger value : %d\n", max_val);
    
        return 0;
    }
    
    (venv) [ttucker@zim stackoverflow]$ gcc -o min_max min_max.c 
    (venv) [ttucker@zim stackoverflow]$ ./min_max 
    
    Input the length of the array:4
    
    Input the array element:
    9
    8
    2
    6
    Smallest value : 2
    Larger value : 9