dev_cc
dev_cc

Reputation: 71

Write a C program that accepts some integers from the user and find the highest value and the input position

i inputed 5 numbers 8,10,25,75,100.If we refer to if(number[j]>max)=>(number[0]>max)=>(8>0),result might be 8.But exact answer is 100,why?

#include <stdio.h>
#define MAX 5
int main() 
{
int number[MAX], i, j, max=0, num_pos=0;
printf("Input 5 integers: \n");
for(i = 0; i < MAX; i++) {
    scanf(" %d", &number[i]);
}

for(j = 0; j < MAX; j++) 
{
    if(number[j] > max) {
        max = number[j];
        num_pos = j;
    }
}
printf("Highest value: %d\nPosition: %d\n", max, num_pos+1);
return 0;
}

Upvotes: 0

Views: 931

Answers (1)

Acorn
Acorn

Reputation: 26066

If we refer to if(number[j]>max)=>(number[0]>max)=>(8>0),result might be 8.

Yes, 8 > 0, which is the first iteration.

In the next one, you will have 10 > 8, which will also be true.

Until you reach 100 > 75, and 100 will be the highest.

Upvotes: 2

Related Questions