寫一個program要user入5個數
最後output要出到邊個數最大同埋係第幾個數最大,下面就係答案,但係有小小野唔明
#include <stdlib.h>
#include <stdio.h>
int main (){
int i, j, num[5], max=0, pos=0;
printf("Input 5 integer: \n");
for(i=0; i<5; i++)
{
scanf(" %d", &num[i]);
}
for(j=0; j<5; j++)
{
if(num[j]>max)
{
max = num[j];
pos = j;
}
}
printf("Highest value: %d\n", max);
printf("Position: %d", pos+1);
system("pause");
return 0;
}
呢個program最後可以完全成功做到哂要求但唔太明點解呢段code,點解max set左做0,但又會用num[j]去同max比較之後又會出得番最大的數?有冇高手可以解到?thank you
if(num[j]>max)
{
max = num[j];
pos = j;
} |