Thread: the bubble sort doesn't work help

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    Unhappy 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])
    {
    /*load up person arrays*/
    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))
    {
    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][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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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))
    {
    printf("please re-enter the age data\n\r");
    scanf("%d",&person[array1][3]);
    }


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

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Would you PLEASE read replies?
    Would you PLEASE use [code][/code]-tags?
    Would you PLEASE read the reader?

    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])
    {
      /*load up person arrays */
      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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. bubble sort
    By nynicue in forum C Programming
    Replies: 7
    Last Post: 04-15-2009, 05:09 AM
  3. problem with gets and bubble sort
    By wwwildbill in forum C Programming
    Replies: 4
    Last Post: 04-04-2009, 01:31 AM
  4. Alternate energy sources
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-02-2005, 07:07 PM
  5. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 06:27 AM