How to output the index of my array in C?

1

I have a program that will check 9 numbers and say which one is the largest of all, and for this I am using vetor already filled with values, and am using a for to go through all vetor e look for the greater. The problem is that I am not able to show which index number is the largest of them all.

My code:

setlocale(LC_ALL,"portuguese");

    int bolas[9] = {0,0,0,0,20,0,0,0,0};

    for(int i = 0; i < 9; i++) {
        if(bolas[i] > bolas[i+1]) {
            printf("A bola %d é maior que todas as outras. ",bolas[i]);
        }
    }

In this code, in the part of printf it will show the size of the number, not its position in the index, I need to show the position and not the size. How can I do this?

    
asked by anonymous 24.06.2017 / 15:45

1 answer

3

If you are giving printf within for , it is writing the message several times. What you need to do is, first find which index has the highest value, and then for make the impression ( printf ). Example:

int bolas[9] = {0,0,0,0,20,0,0,0,0};
int maior = 0;
int i = 1;
for(i = 1; i < 9; i++) {
    if(bolas[i] > bolas[maior]) {
        maior = i;
    }
}

printf("A bola %d é maior que todas as outras. ",bolas[maior]);

or if you want to print only the index, not the value of the ball:

printf("A bola %d é maior que todas as outras. ",maior);
    
24.06.2017 / 15:59