Home > OS >  False min and max assignments in function
False min and max assignments in function

Time:12-12

I have a function which is supposed to find min and max in an array using struct. But somehow the function assigns wrong values to min and max variables. Could someone please explain where do I have the mistake? Thank you very much. P.S. In my assignment the function doesn't need to take the first element of the array

min_max_t min_max(unsigned int *array, int size)
{
    min_max_t flag;
    flag.min = array[1];
    flag.max = array[1];
    printf("Flag.min: %d | ", flag.min);
    printf("Flag.max: %d\n", flag.max);
    for (int i = 1; i < size; i  )
    {
        printf("i = %d - [A:%d - Min:%d - Max:%d]\n", i, array[i], flag.min, flag.max);
        if(array[i] > flag.max)
        {
            flag.max = array[i];
        }
        else if (array[i] < flag.min)
        {
            flag.min = array[i];
        }
        printf("i = %d - [A:%d - Min:%d - Max:%d]\n\n", i, array[i], flag.min, flag.max);
    }
    return flag;
}

Screenshot of function process

CodePudding user response:

  • The indexes start at 0, not at 1.

    flag.min = flag.max = array[0];
    
  • When you find a new max, you ignore to set the possible flag.min -- it may be possible for them to be equal (make 2 if-conditions, without else).

    else if (array[i] < flag.min)
    
  • Maybe you want to print intermediate results only at the end of the loop.

  • To print min and max before the loop, it does not make too much sense.

    printf("Flag.min: %d | ", flag.min);
    

CodePudding user response:

There is nothing wrong with the logic for finding min and max.

The problem with your code is that you print unsigned int using %d. When printing unsigned int values use %u.

Another issue, that you may consider to handle, is illegal values of the function parameter size. Your function requires that size is at least 2. To avoid undefined behavior you may want to check for that.

In the start of the function you could for instance add

assert(size >= 2);

or

if (size < 2)
{
    // return some suitable value
}

That said you can also just document the function to require size being at least 2. In C it's not uncommon to set such contract requirements for functions. Several stdlib functions have such requirements.

BTW: If you add check for size you should probably also check for array not being NULL.

BTW: Your Screenshot indicates that you pass an array of int to the function. If that's true then you have a bug in the caller-code. Don't pass an array of int to a function expecting an an array of unsigned int.

CodePudding user response:

For starters the function should be declared like

min_max_t min_max( const unsigned int *array, size_t size );

and the structure min_max_t should contain two data members that will store indices to minimum and maximum elements. For example

typedef struct min_max_t
{
    size_t min;
    size_t max;
} min_max_t;

Otherwise the function can invoke undefined behavior when for example the user passed as the second argument 0.

Indices in arrays start from 0. So you skipped within the function the first element of the passed array.

As the array has elements of the type unsigned int then the expression -1 is converted implicitly to the maximum value of the type unsigned int. So you need to decide whether indeed you want to deal with unsigned integer arrays or with signed integer arrays.

Using the conversion specifier %d instead of %u in a call of printf to output an object of the type unsigned int can invoke undefined behavior if the value of the object does not fit in an object of the type int.

So your function can look the following way

typedef struct min_max_t
{
    size_t min;
    size_t max;
} min_max_t;

min_max_t min_max( const unsigned int *array, size_t size )
{
    min_max_t flag = { .min = 0, .max = 0 };

    printf( "Flag.min: %zu | ", flag.min );
    printf( "Flag.max: %zu\n", flag.max );

    for ( size_t i = 1; i < size; i   )
    {
        printf( "i = %zu - [A:%u - Min:%u - Max:%u]\n", i, array[i], array[flag.min], array[flag.max] );

        if ( array[flag.max] < arra[i] )
        {
            flag.max = i;
        }
        else if ( array[i] < array[flag.min] )
        {
            flag.min = i;
        }

        printf( "i = %zu - [A:%u - Min:%u - Max:%u]\n\n", i, array[i], array[flag.min], array[flag.max] );
    }

    return flag;
}
  • Related