C 如果语句未执行,则冒泡排序根本不起作用
我正在开发一个程序,该程序将用于对学生的考试分数进行排序,并最终检索分数的平均值、中位数和模式。由于某种奇怪的原因,我的气泡排序不起作用。。我不知道为什么C 如果语句未执行,则冒泡排序根本不起作用,c,visual-studio-2010,C,Visual Studio 2010,我正在开发一个程序,该程序将用于对学生的考试分数进行排序,并最终检索分数的平均值、中位数和模式。由于某种奇怪的原因,我的气泡排序不起作用。。我不知道为什么 #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 3 int main (void) { char vStudents[N][15], trans = 'y', vTemp2; int vScores[N], vT
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
int main (void)
{
char vStudents[N][15], trans = 'y', vTemp2;
int vScores[N], vTemp, x, i = 0, j=0, NewN;
printf("\t\tWhatsamatta U Scores System\n\n");
do
{
printf("Please Enter Students Name: ");
gets(vStudents[i]);
trans = 'N';
while (trans == 'N')
{
printf("Enter Students Score: ");
scanf("%d", &vScores[i]);
fflush(stdin);
if (vScores[i] >= 0 & vScores[i] <= 100)
trans = 'y';
else
printf("Score is invalid, please re-enter score.\n");
}
i++;
j++;
} while (j != N);
for(x = 0; x < N - 1; x++)
{
if ((x < N - 1) && (vScores[i] > vScores[i + 1]))
{
vTemp = vScores[i];
vScores[i] = vScores[i + 1];
vScores[i + 1] = vTemp;
x = -1;
}
}
printf("%d %d %d\n\n", vScores[0], vScores[1], vScores[2]);
system("Pause");
return 0;
#包括
#包括
#包括
#定义n3
内部主(空)
{
char v学生[N][15],trans='y',vTemp2;
intvscores[N],vTemp,x,i=0,j=0,NewN;
printf(“\t\tWhatsamatta U分数系统\n\n”);
做
{
printf(“请输入学生姓名:”);
获取(vstudens[i]);
trans='N';
而(trans='N')
{
printf(“输入学生分数:”);
scanf(“%d”和&vScores[i]);
fflush(stdin);
if(vScores[i]>=0&vScores[i]vScores[i+1]))
{
vTemp=vScores[i];
vScores[i]=vScores[i+1];
vScores[i+1]=vTemp;
x=-1;
}
}
printf(“%d%d%d\n\n”,vScores[0],vScores[1],vScores[2]);
系统(“暂停”);
返回0;
任何帮助都会很有用,请提前感谢!至少有一个错误:
for(x = 0; x < vScores[N] - 1; x++)
{
if ((x < vScores[N] - 1) && (vScores[N] > vScores[N + 1]))
{
(x=0;x
{
if((xvScores[N+1]))
{
应该是
for(x = 0; x <N - 1; x++)
{
if ((x < N - 1) && (vScores[N] > vScores[N + 1]))
{
//^^you should not compare index x with array elements
for(x=0;x个vScores[N+1]))
{
//^^不应将索引x与数组元素进行比较
至少有一个错误:
for(x = 0; x < vScores[N] - 1; x++)
{
if ((x < vScores[N] - 1) && (vScores[N] > vScores[N + 1]))
{
(x=0;x
{
if((xvScores[N+1]))
{
应该是
for(x = 0; x <N - 1; x++)
{
if ((x < N - 1) && (vScores[N] > vScores[N + 1]))
{
//^^you should not compare index x with array elements
for(x=0;x个vScores[N+1]))
{
//^^不应将索引x与数组元素进行比较
N始终是3。如果我们将代码中的N替换为3,它仍然有意义吗
for(x = 0; x < vScores[3] - 1; x++)
{
if ((x < vScores[3] - 1) && (vScores[3] > vScores[3 + 1]))
{
vTemp = vScores[3];
vScores[3] = vScores[3 + 1];
vScores[3 + 1] = vTemp;
x = -1;
}
}
(x=0;x
{
if((xvScores[3+1]))
{
vTemp=vScores[3];
vScores[3]=vScores[3+1];
vScores[3+1]=vTemp;
x=-1;
}
}
好的,现在是这样:
for(x = 0; x < N - 1; x++)
{
if ((x < N - 1) && (vScores[i] > vScores[i + 1]))
{
vTemp = vScores[i];
vScores[i] = vScores[i + 1];
vScores[i + 1] = vTemp;
x = -1;
}
}
(x=0;x
{
if((xvScores[i+1]))
{
vTemp=vScores[i];
vScores[i]=vScores[i+1];
vScores[i+1]=vTemp;
x=-1;
}
}
问:
i
什么时候改变?N总是3。如果我们用3替换代码中的N,它仍然有意义吗
for(x = 0; x < vScores[3] - 1; x++)
{
if ((x < vScores[3] - 1) && (vScores[3] > vScores[3 + 1]))
{
vTemp = vScores[3];
vScores[3] = vScores[3 + 1];
vScores[3 + 1] = vTemp;
x = -1;
}
}
(x=0;x
{
if((xvScores[3+1]))
{
vTemp=vScores[3];
vScores[3]=vScores[3+1];
vScores[3+1]=vTemp;
x=-1;
}
}
好的,现在是这样:
for(x = 0; x < N - 1; x++)
{
if ((x < N - 1) && (vScores[i] > vScores[i + 1]))
{
vTemp = vScores[i];
vScores[i] = vScores[i + 1];
vScores[i + 1] = vTemp;
x = -1;
}
}
(x=0;x
{
if((xvScores[i+1]))
{
vTemp=vScores[i];
vScores[i]=vScores[i+1];
vScores[i+1]=vTemp;
x=-1;
}
}
问:
i
什么时候更改?自从使用visual studio以来,您是否尝试过visual studio非常好的调试器逐行检查变量?是的,我尝试过,但调试器跳过冒泡排序所在的部分。它显示for循环的开始,但直接跳到bot处的printftom。调试器没有说谎。这意味着for循环根本没有执行。可能for循环执行的条件第一次是false。好吧,现在循环执行,但是if语句没有。我不确定为什么,因为if所要求的是true。我建议如果if语句没有执行,则条件正在评估为False自从您使用visual studio以来,您是否尝试过visual studio非常好的调试器逐行检查变量?是的,我尝试过,但调试器跳过冒泡排序所在的部分。它显示for循环的开始,但直接跳到底部的printf。调试器确实如此不是谎言。这意味着您的for循环根本没有被执行。可能for循环第一次执行的条件是false。好吧,循环现在执行,但是if语句没有。我不确定为什么,因为if所要求的是true。我建议如果if语句没有执行,则条件的计算结果是false谢谢!我已经这样做了,可能是5或6分钟前,但是冒泡排序仍然没有执行..我想我应该快速更新代码谢谢!我已经这样做了,可能是5或6分钟前,但是冒泡排序仍然没有执行..我想我应该快速更新代码