1
I have a program that will check 9 numbers and tell which one is the biggest of all, and for this I am using vetor
already filled with values, and I’m using a for
to go through the whole vetor
and search for the largest. The problem is that I’m not able to show which index number is the largest of 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 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 ?
It does not work your code, it returns the same thing as mine. See working : http://ideone.com/UBrrhy.
– Monteiro
Want the index? Have it written
maior
, because this is the index of the largest element of the vector– Jefferson Quesado
works!,
stdout: A bola 20 é maior que todas as outras.
– Rovann Linhalis
see the change in response
– Rovann Linhalis
Thanks, it worked even, only a correction, as the index of the array starts with 0, you have to put +1 in the largest to give the true index.
– Monteiro
printf("Ball[%d] = %d eh the largest of all", largest, ball[largest]) will display both the index and the largest element, @Monteiro
– Jefferson Quesado
@Mount indexes start at 0 or 1? In fact, it depends on the semantic set you are working on, so for the semantic set vector c, the given solution is fully correct
– Jefferson Quesado
in fact, the true index would continue to start with 0 né...rs but if the user wants to display by 1, ok, just add +1 to the index
– Rovann Linhalis
but it’s cool, I was about 4 years old who did not mess with c rsrs
– Rovann Linhalis
Thanks guys, I’m new to C, so fine, I’ll take your word for it because you have more experience than I do. And it worked right the code. + 1 and right answer.
– Monteiro