I can not find the indexes of the largest number

0

I'm doing a program that needs to know the indexes of the largest values in the vector, but I'm not getting the code I tried.

#include <stdio.h>

int main(int argc, char** argv)
{
  double vetor[10];
  int indice[10], c = 0;
  for(int i = 0; i < 10; i++)
  {
    scanf("%lf", &vetor[i]);
  }
  double maior = vetor[0];
  for(int i = 0; i < 10; i++)
  {
     if(vetor[i] > maior)
     {
        maior = vetor[i];
        indice[c] = i + 1;
        c++;
     }
  }
  for(int i = 0; i < c; i++)
  {
     printf("%d ", indice[i]);
  }
   return 0;
}
    
asked by anonymous 06.04.2018 / 21:28

1 answer

0

You should start the variable maior with a value different from what is already in the vector, say for example -DBL_MAX (defined in float.h). That way you know that anything that comes will be bigger (probably).

In addition when storing the index does not need to add 1 do:

indice[c] = i;
    
06.04.2018 / 21:51