#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;
}
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]的比较,这时数组越界了,这也是一个问题。
这段代码运行是时候总是提示异常