#include
int main()
{
int min = 0;
int max = 0;
int arr[20];
int i = 0;
scanf("%d", &arr[i]);
min = max = arr[0];
do
if(arr[i] > max)
max = arr[i];
if(arr[i] < min)
min = arr[i];
scanf("%d", &arr[++i]);
} while (i < 19);
printf("max is %d, min is %d", max, min);
return 0;
}
1、陣列越界
2、找最大最小應該是每個都與最終的最大最小值比較的
如果不考慮特殊的輸入值,可以給min和max初始值,程式碼會簡單很多:
int min = 100000000;
int i;
for(i = 0; i < 20; ++i)
printf("max is %d, min is %d\n", max, min);
#include
int main()
{
int min = 0;
int max = 0;
int arr[20];
int i = 0;
scanf("%d", &arr[i]);
min = max = arr[0];
do
{
if(arr[i] > max)
max = arr[i];
if(arr[i] < min)
min = arr[i];
scanf("%d", &arr[++i]);
} while (i < 19);
if(arr[i] > max)
max = arr[i];
if(arr[i] < min)
min = arr[i];
printf("max is %d, min is %d", max, min);
return 0;
}
1、陣列越界
2、找最大最小應該是每個都與最終的最大最小值比較的
如果不考慮特殊的輸入值,可以給min和max初始值,程式碼會簡單很多:
#include
int main()
{
int min = 100000000;
int max = 0;
int arr[20];
int i;
for(i = 0; i < 20; ++i)
{
scanf("%d", &arr[i]);
if(arr[i] > max)
max = arr[i];
if(arr[i] < min)
min = arr[i];
}
printf("max is %d, min is %d\n", max, min);
return 0;
}