I’m having trouble finding the highest number

Asked

Viewed 40 times

0

Well I’m doing a program that needs to know the indexes of the highest 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;
}
  • Your goal is to find the index only of the highest number ? If yes why indice is a vector with 10 positions ?

  • It is not only of the largest number, but of the largest numbers of the vector

  • Of the greatest ? But how many ? By their example seem to be all 10, which wouldn’t make much sense. Better try to clarify and give an example of input numbers and output result.

  • numbers 1 2 3 4 5 6 7 8 9 10 indices of the highest values 0 1 2 3 4 5 6 7 8 9

  • Then the 0 is the first word to appear, but in the position 0 is the lowest value. There is no confusion ?

1 answer

0

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

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

indice[c] = i;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.