Thread: Bubblesort malfunctioning

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    18

    Talking Bubblesort malfunctioning

    #include<stdio.h>

    int main()
    {
    int /* PersonID,Wage,Sex,Age, */ n, i;
    int a[10][4];
    printf("please enter the amount of people you will use \n");
    scanf("%d",&n);
    printf("\n=%d\n\n", n);
    for (i=0; i<n; i++)
    {
    printf("please enter the Person ID (person %d)\n", n);
    scanf("%d",&a[i][0]);
    printf("please enter the Wage (person %d)\n", n);
    scanf("%d",&a[i][1]);
    printf("please enter the Age (person %d)\n", n);
    scanf("%d",&a[i][2]);
    printf("please enter the Sex use 1 for male and 0 for female (person %d)\n", n);
    scanf("%d",&a[i][3]);
    }

    printf("\t Survey Data:\n");
    printf("\t~~~~~~~~~~~~~~~~~~~\n");
    for (i=0; i<n; i++)
    {
    printf("Person %d\t", i);
    printf("ID: %d\t", a[i][0]);
    printf("Wage: %d\t", a[i][1]);
    printf("Sex: %d\t", a[i][2]);
    printf("Age: %d\t\n", a[i][3]);
    }

    int t;
    char sort;
    t=1;
    while (t==1)
    {
    printf("\n\rHow do you want 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, print d");
    printf("\n\rOnly males, print e\n\r");
    scanf("\n%c",&sort);
    if (sort='a')/* Sort for ID*/
    {
    {
    int i, j, temp;
    for( j = 0; j < n - 1; j++ )
    for( i = 0; i < n - j + 1; j++ )
    if( a[i][0] > a[i + 1][0] )


    {
    temp = a[i][0];
    a[i][0] = a[i + 1][0];
    a[i + 1][0] = temp;
    }
    }

    }
    for (i=0; i<n; i++)
    {
    printf("Person %d\t", i);
    printf("ID: %d\t", a[i][0]);
    printf("Wage: %d\t", a[i][1]);
    printf("Sex: %d\t", a[i][2]);
    printf("Age: %d\t\n", a[i][3]);
    }
    printf("Would you like to sort the data again Yes = 1 No = 0 \n");
    scanf("%d",&t);
    }return 0;
    }


    The sort is only operationional under option a haven't implemented the others yet........... basically the sort only passes the first 2 numbers entered into the array can someone have a look at this for me thanks !

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do a board search for bubble sort

    > int t;
    > char sort;
    This inline declaration smacks of C++ - where is your function called sort?

    > if (sort='a')/* Sort for ID*/
    And this should be
    if (sort=='a')/* Sort for ID*/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem: Bubblesort - "strlen" in the function...
    By Petike in forum C Programming
    Replies: 15
    Last Post: 01-24-2008, 09:20 PM
  2. scanf in bubblesort
    By agentsmith in forum C Programming
    Replies: 3
    Last Post: 12-18-2007, 05:09 PM
  3. Circular bubblesort
    By PiCoMiKe in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 06:06 PM
  4. C++ bubblesort and structure help
    By ReignFire in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 02:25 AM
  5. bubblesort
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 10:45 PM