C語言入門 數組的應用問題
#include<stdio.h>
int main()
{
int shengao[] = { 172,163,179,180,165,168,172 };
int q , r;
for (q = 6; q>= 0; q--)
{
for (r = 0; r <= q; r++)
{
if (shengao[r] > shengao[r + 1])
{
int temp;
temp = shengao[r];
shengao[r] = shengao[r + 1];
shengao[r + 1] = temp;
}
}
}
for (q = 0; q < 7; q++)
{
if (q != 6)
{
printf("%d,", shengao[q]);
}
else
{
printf("%d\n", shengao[q]);
}
}
return 0;
}
2016-12-03
for (q = 6; q>= 0; q--)
{
for (r = 0; r <= q; r++)
q = 6不對。數組里面7個元素,外層for只要循環6次就夠,而q從0-6卻循環了7次,這就出問題了;
而且對內層的for循環,r <= q即r<=6,循環到r=6,會出現了shengao[6]> shengao[7]的比較,這時數組越界了,這也是一個問題。
2016-11-30
這段代碼運行是時候總是提示異常