Home > OS >  Returning largest number from an array using an int function not printing value
Returning largest number from an array using an int function not printing value

Time:10-26

I am attempting to create a small program that receives an array and returns the largest number from the array. I was able to create a program using a function with the initializer "void", but do not understand why I can't do the same with a different function using "int".

I get the error "incompatible pointer types returning 'int *(int *, int *)' from a function with result type 'int *'" when using the function int largestNum(int *length,int numberArr[]). Any explanation is appreciated.

Input:
Element 0 - 23
Element 1 - 12
Element 2 - 34

Output: 34

Function using void:

void largestNum(int *length,int numberArr[]){

 int largestNumber = numberArr[0];

 for(int x = 1;x < *length;x  ){
   if (numberArr[x] > largestNumber)
   largestNumber = numberArr[x];
  }
  printf("%d", largestNumber);
 }


int main(){

 int numbersArr[100];
 int input;
 int num;
 int count = 0;
 
 printf("Input total number of elements (1-100): ");
 scanf("%d", &num);

 for(int x = 0; x < num; x  ){
   printf("Element %d - ", count);
   scanf("%d", numbersArr   x);
   count  ;
 }


 largestNum(&count, numbersArr);

 return 0;
 }

Function using int:

int largestNum(int *length,int numberArr[]){

 int largestNumber = numberArr[0];

 for(int x = 1;x < *length;x  ){
   if (numberArr[x] > largestNumber)
   largestNumber = numberArr[x];
 }
 return largestNumber;
} 


int main(){

 int numbersArr[100];
 int input;
 int num;
 int count = 0;

 printf("Input total number of elements (1-100): ");
 scanf("%d", &num);

 for(int x = 0; x < num; x  ){
   printf("Element %d - ", count);
   scanf("%d", numbersArr   x);
   count  ;
 }


  largestNum(&count, numbersArr);

  printf(largestNum);

 return 0;
 }

CodePudding user response:

You need to keep the value in a variable like:

a = largestNum(&count, numberArr);
printf("%d", a);

Or you can just call the function in the printf

printf("%d", largestNum(&count, numberArr))

CodePudding user response:

largestNum is a function declared like

int largestNum(int *length,int numberArr[]);

So in this call

printf(largestNum);

the function designator is implicitly converted to pointer of the type int( int *, int * ) but the function printf expects the first argument of the type char *. That is the above call of printf does not make a sense. You need to output the returned value of the function. For example

printf("%d\n", largestNum(&count, numbersArr) );

Pay attention to that in general the function is incorrect. It can invoke undefined behavior when the value of the expression *length is equal to 0.

Also there is no great sense to pass the first argument by reference. And as the array is not being changed within the function it should be declared with the qualifier const.

The function can be declared and defined the following way

size_t largestNum( const int numberArr[], size_t n )
{
    size_t largestNumber = 0;

    for ( size_t i = 1; i < n; i   )
    {
        if ( numberArr[largestNumber] < numberArr[i] )
        {
            largestNumber = i;
        }
    }
    
    return largestNumber;
} 

And the function can be called like

size_t pos = largestNum( numbersArr, count );
printf("%d\n", numbersArr[pos] );

To check an error when the second parameter was specified equal to 0 you can write

size_t pos = largestNum( numbersArr, count );
if ( pos != count ) printf("%d\n", numbersArr[pos] );
  • Related