Reputation: 47
I'm working on a program which is going to be used to sort students test scores and eventually retrieve the mean, median, and the mode of the scores. For some strange reason my bubble sort is not working.. I'm unsure why.
#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;
Any help would be useful, thanks in advance!
Upvotes: 0
Views: 133
Reputation: 1161
N is always 3. if we replace N in your code with 3, does it still make sense?
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;
}
}
Ok, now that it is this:
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;
}
}
Ask, when does i
change?
Upvotes: 1
Reputation: 23654
At least one error:
for(x = 0; x < vScores[N] - 1; x++)
{
if ((x < vScores[N] - 1) && (vScores[N] > vScores[N + 1]))
{
should be
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
Upvotes: 4