我正在开发一个程序,该程序将用于对学生的考试分数进行排序,并最终检索出分数的均值,中位数和模式。 由于某些奇怪的原因,我的气泡排序无法正常工作。.我不确定为什么。

#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;

任何帮助将是有用的,在此先感谢!

===============>>#1 票数:4

至少一个错误:

for(x = 0; x < vScores[N] - 1; x++)
{   
   if ((x < vScores[N] - 1) && (vScores[N] > vScores[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

===============>>#2 票数:1

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;
    }
}

好的,现在是这样的:

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;
    }
}

问, i什么时候换?

  ask by user2084990 translate from so

未解决问题?本站智能推荐: