Home > Software engineering >  Finding the Minimum of Array while passing by reference
Finding the Minimum of Array while passing by reference

Time:05-30

I am trying to write a void Function using pass by reference , that finds the minimum of an Array while only giving a pointer of an Array and the length of the Array as a Parameter into the Function.

I keep getting errors like:

Incomplete type is not allowed (in the main function) assignment to 'int *' from 'int' makes pointer from integer without a cast.

This is my code:

void minimum(int *iArray[], int count)
{

    int min = iArray[0];
    for(int i = 1; i < count; i  )
    {
        if (min > iArray[i])

        *iArray = min;

    }

}


int main()
{
    int numbers[] = {4, 6, 9, 5};

    printf("%d", minimum(numbers, 4));

    return 0;
}

Can you help me out how i can do it better?

CodePudding user response:

numbers is pointer to first element of the array. Thus your function definition should be like :-

int minimum(int *iArray, int count)

or

int minimum(int iArray[], int count)

Note: Your logic for finding minimum has some errors and you are supposed to return the min for it to be printed with printf. Your complete code will look like :-

#include <stdio.h>

int minimum(int *iArray, int count)
{

    int min = iArray[0];
    for(int i = 1; i < count; i  )
    {
        if (min > iArray[i])
            min = iArray[i];

    }
    return min;
}


int main()
{
    int numbers[] = {4, 6, 9, 5};

    printf("%d", minimum(numbers, 4));

    return 0;
}

CodePudding user response:

The declaration and the definition of the function are incorrect.

In this function call

minimum(numbers, 4)

the array is implicitly converted to pointer to its first element of the type int *. But the corresponding function parameter has the type int *[] that is adjusted by the compiler to the type int **..

Thus for example in this line

int min = iArray[0];

the object min of the type int is initialized by a pointer of the type int *.

Also it is a bad idea to change the source array in a function that finds the minimal element.

And as the function has the return type void then the call of the function printf is invalid

printf("%d", minimum(numbers, 4));

The function should be declared and defined the following way

int * minimum( const int *a, size_t n )
{
    const int *min = a;

    for ( size_t i = 1; i < n; i   )
    {
        if ( a[i] < *min ) min = a   i;
    }

    return ( int * )min;
}

And the call of the function will look like

int *min = minimum(numbers, 4);

printf( "The minimum value is %d at position %tu.\n", *min, min - numbers );

Alternatively the function can be declared and defined the following way

size_t minimum( const int *a, size_t n )
{
    size_t min = a;

    for ( size_t i = 1; i < n; i   )
    {
        if ( a[i] < a[min] ) min = i;
    }

    return min;
}

And the call of the function will look like

size_t min = minimum(numbers, 4);

printf( "The minimum value is %d at position %zu.\n", numbers[min], min );
  • Related