Functions with parameters in C language

0

I need help solving the following problem:

"Write a function that gets an array of integers as a parameter and returns the index of the largest element of the array.

I made a part of the code and it does what was requested, but not in the way it was requested:

int recebeArray(){
  int mtrx[5];
  int count, maior;

  for(count=0; count<5; count++){
    printf("Digite um numero \n");
    scanf("%d",&mtrx[count]);

    if(mtrx[count] > mtrx[count-1]){        
      maior=mtrx[count];
    }           
  }

  printf("O maior numero e' : %d",maior);
}

int main(){
  recebeArray();
}
    
asked by anonymous 06.11.2017 / 10:11

1 answer

3

You need to go through all elements of the vector, storing the index containing the largest value already found in an auxiliary variable, for example:

#include <stdio.h>

int intMax( int *a, int tamanho )
{
    int i, max = 0;
    for( i = 1; i < tamanho; i++ )
        if( a[i] > a[max] )
            max = i;
    return max;
}

int main( void )
{
    int foobar[8] = { 4, 10, 2, 13, 3, 7, 1, 0 };
    int i = intMax( foobar, 8 );
    printf( "foobar[%d] = %d\n", i, foobar[i] );
    return 0;
}

Output:

foobar[3] = 13
    
06.11.2017 / 10:27