# Thread: the bubble sort doesn't work help

1. ## the bubble sort doesn't work help

thanks to all that have looked at this but i still can't get the bubble sort to work, please help or i am F***ed, if i can see it coded then i will be able to see were i was going wrong. heres the code, I'm a beginner by the way(in case you cant tell. lol)
#include<stdio.h>
void datain(int, int[10000][3]);
void dataout(int, int[10000][3]);
void main()

{

int down,person[10000][3];

/*choose the size of table required*/
printf("Please enter the amount of the people required: \n\r");
scanf("%d",&down);
printf("Amount of people required: %d\n\r",down);

/*validate user input*/
while ((down <=0) || (down > 100000))
{
printf("please re-enter the number of people required:\n\r");
scanf("%d",&down);
}

/*Create table and input data*/
datain(down, person);

/*print out the table unsorted*/
dataout(down, person);

}
void datain(int numberofpeople, int person[10000][3])
{
int array1;

for (array1 = 1; array1 <= numberofpeople; array1 ++)
{
printf("\n\rPlease input the person identification: ");
scanf("%d",&person[array1][0]);

printf("Please input the person Wage: ");
scanf("%d",&person[array1][1]);

printf("Please input the person Sex(0 is male and 1 is female): ");
scanf("%d",&person[array1][2]);
/*validate user input*/
while ((person[array1][2] < 0) || (person[array1][2] > 1))
{
scanf("%d",&person[array1][2]);
}

printf("Please input the person Age: ");
scanf("%d",&person[array1][3]);
/*validate user input*/
while ((person[array1][3] < 16) || (person[array1][3] > 65))
{
scanf("%d",&person[array1][3]);
}
}
}

void dataout (int number,int person[10000][3])
{
int array;
printf(" \n\r The Unsorted Data:\n");
printf(" ---------------------\n\r");
printf("Personnal ID Wage Sex Age\n\r");
for (array=1; array<=number; array++)
{
printf("%8d%8d%8d%8d\n\r",person[array][0],person[array][1],person[array][2],person[array][3]);
}
/*select form of display from user*/
char nochmal;
char sort;
nochmal=1;
while (nochmal==1)
{
/*select display from user*/
printf("\n\rHow is the data to be sorted ?");
printf("\n\rAscending by Person ID,type a");
printf("\n\rAscending by Wage, type b");
printf("\n\rAscending by Age, type c");
printf("\n\rOnly females, type f");
printf("\n\rOnly males, type m\n\r");
scanf("\n%c",&sort);
/* Sort the data*/
{
if (sort == 'a')
if (sort == 'b')
if (sort == 'c')
if (sort == 'd')
if (sort == 'f')
if (sort == 'm');
}
int array, index, total;
for( index = 0; index < array - 1; index ++ )
for( array = 0; array < array - index + 1; index ++ )
if( person[array][0] > person[array + 1][0] )
{
total = person[array][0];
person[array][0] = person[array + 1][0];
person[array + 1][0] = total;
}

for (array = 0; array < array; array ++)
{
printf("Personnel ID: %d\t", person[array][0]);
printf("Wage: %d\t", person[array][1]);
printf("Sex: %d\t", person[array][2]);
printf("Age: %d\t\n", person[array][3]);
}
/*to resort the data*/
printf("Would you like to sort the data again Yes = y No = n \n");
scanf("%d",&nochmal);

return;
}

}

2. Do you even read the replies you're given? How many times do I have to say it, your array indexing is WRONG:

for (array1 = 1; array1 <= numberofpeople; array1 ++)

Should be:

for (array1 = 0; array1 < numberofpeople; array1 ++)

WRONG:

printf("Please input the person Age: ");
scanf("%d",&person[array1][3]);
/*validate user input*/
while ((person[array1][3] < 16) || (person[array1][3] > 65))
{
scanf("%d",&person[array1][3]);
}

There is no array[x][3] !! It is ONLY: array[x][0] to array[x][2]

Quzah.

Here's "working" code I fixed only C syntax and indexing errors. The sort algorithm works, but only sorts the ID's. I guess programming will never be a hobby of yours?

Code:
```#include<stdio.h>

#define NOCHMAL 'y'

void datain (int, int[10000][4]);
void dataout (int, int[10000][4]);

int
main (void)
{
int down, person[10000][4];

/*choose the size of table required */
printf ("Please enter the amount of the people required:\n\r");
scanf ("%d", &down);
printf ("Amount of people required: %d\n\r", down);
/*validate user input */
while ((down <= 0) || (down > 10000))
{
printf ("please re-enter the number of people required:\n\r");
scanf ("%d", &down);
}

/*Create table and input data */
datain (down, person);
/*print out the table unsorted */
dataout (down, person);

return 0;
}

void
datain (int numberofpeople, int person[10000][4])
{
int array1;
for (array1 = 0; array1 < numberofpeople; array1++)
{
printf ("\n\rPlease input the person identification: ");
scanf ("%d", &person[array1][0]);
printf ("Please input the person Wage: ");
scanf ("%d", &person[array1][1]);
printf ("Please input the person Sex(0 is male and 1 is female):");
scanf ("%d", &person[array1][2]);
/*validate user input */
while ((person[array1][2] < 0) || (person[array1][2] > 1))
{
printf ("please re-enter the sex data\n\r");
scanf ("%d", &person[array1][2]);
}

printf ("Please input the person Age: ");
scanf ("%d", &person[array1][3]);
/*validate user input */
while ((person[array1][3] < 16) || (person[array1][3] > 65))
{
printf ("please re-enter the age data\n\r");
scanf ("%d", &person[array1][3]);
}
}
}

void
dataout (int number, int person[10000][4])
{
int array;
char nochmal;
char sort;
int index, total;

printf (" \n\r The Unsorted Data:\n");
printf (" ---------------------\n\r");
printf ("Personnal ID Wage Sex Age\n\r");
for (array = 0; array < number; array++)
{
printf ("%8d%8d%8d%8d\n\r", person[array][0],
person[array][1], person[array][2], person[array][3]);
}
/*select form of display from user */
nochmal = NOCHMAL;
while (nochmal == NOCHMAL)
{
/*select display from user */
printf ("\n\rHow is the data to be sorted ?");
printf ("\n\rAscending by Person ID,type a");
printf ("\n\rAscending by Wage, type b");
printf ("\n\rAscending by Age, type c");
printf ("\n\rOnly females, type f");
printf ("\n\rOnly males, type m\n\r");
scanf (" %c", &sort);
/* Sort the data */
/*
if (sort == 'a')
if (sort == 'b')
if (sort == 'c')
if (sort == 'd')
if (sort == 'f')
if (sort == 'm');
*/

for (index = 0; index < number; index++)
for (array = 0; array < number - 1; array++)
if (person[array][0] > person[array + 1][0])
{
total = person[array][0];
person[array][0] = person[array + 1][0];
person[array + 1][0] = total;
}

for (array = 0; array < number; array++)
{
printf ("Personnel ID: %d\t", person[array][0]);
printf ("Wage: %d\t", person[array][1]);
printf ("Sex: %d\t", person[array][2]);
printf ("Age: %d\t\n", person[array][3]);
}
/*to resort the data */
printf ("Would you like to sort the data again Yes = y No = n\n");
scanf (" %c", &nochmal);
}
}```
(you are "The Weakest Link"...) Goodbye!