Reputation: 35
Why min
is not compared to *(arr+i)
?
If statement in this code is not being executed
#include <stdio.h>
int main()
{
int i, min;
int arr[5];
for (i = 0; i <= 4; i++)
scanf("%d", &arr[i]);
min = *(arr);
for (i = 1; i <= 4; i++)
{
if ( min < *(arr + i) )
{
printf("min for %d is %d", i, min);
min = *(arr + i);
}
}
printf("%d\n", min);
return 0;
}
Upvotes: 1
Views: 40
Reputation: 7638
The code is executing, the problem is that your condition is wrong:
min=*(arr);
for(i=1;i<=4;i++)
{
if ( min > *(arr+i) ) // Changed < into >
{
printf("min for %d is %d",i,min);
min = *(arr+i);
}
}
You first take the first element as your minimum. Later, for each element, you need to check if your current minimum is larger than the current value. If that is the case, you update.
Upvotes: 2