simple sorting problem

This is a discussion on simple sorting problem within the C Programming forums, part of the General Programming Boards category; Code: [I needed to write a program to implement this sort: for (i=0; i,SIZE; ++i) for (j+i+1; j<SIZE; ++j) if ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    simple sorting problem

    Code:
    [I needed to write a program to implement this sort:
    for (i=0; i,SIZE; ++i)
    for (j+i+1; j<SIZE; ++j)
    if (a[i]> a[j])
    swap (&a[i], &a[j]);}
    [
    then i have to modify it so that all elements of the array are printed after each pass of the outer loop.
    size of array is 8
    and starting values are : 7,3,66,3,-5,22,-77,2
    then the program shoud print the following on the screen:
    
    Unordered data: 7,3,66,3,-5,22,-77,2
    after pass 1: -77,7,66,3,3,22,-5,2
    after pass 2: -77,-5,66,7,3,22,3,2
    
    This is what i have so far:
    
    #include <stdio.h>
    #define order (int*p, int*q);
    int main (void)
    {
    int a[]= {7,3,66,3,-5,22,-77,2};
    int i;
    int j;
    int k;
    printf "("\n Unordered data:");
    for (k=0; k<SIZE;++k)
    PRINTF ("%6D", a [k]);
    printf ("\n")
    for ( i=0; i<SIZE ; ++i)
    for (j+i+1; j<SIZE; ++j)
    order (&a[i], &a[j]);
    printf ("After pass %d:", i+1);
    for (k=0; k<SIZE;k)
    printf("%6d"' (k%2==0)?333:a[k]);
    print ("\n");
    }
    putchar ('\n');
    return 0;
    }
    void order (int *p, int *q)
    {
    int tmp;
    if (*p>*q)
    {
    tmp =*p;
    *p=*q;
    *q=tmp;
    }}
    I'm having problems compiling, getting some errors. Anybody, please lend me another set of eyes or point me in the right direction if i'm going aobut this in the wrong way. Thanks for any help you can give me.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    please fix your post. remove the explanation from the code tags and fix up your indentation. until then i wont be bothering to help. (not trying to be rude or anything)

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    Looks like a classic bubble sort.

    These lines are suspect. I'll leave it to you to figure out what the problem is.
    Code:
    for (j+i+1; j<SIZE; ++j) // just plain wrong
    PRINTF ("&#37;6D", a [k]); // wrong
    #define order (int*p, int*q); // This doesn't do anything
    swap (&a[i], &a[j]); // I'd like to see your swap function this seems overkill
    Last edited by MacNilly; 08-12-2007 at 09:09 PM.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    Code:
    #include <stdio.h>
    void order (int *p, int *q);
    int main (void)
    {
             int a[8]= {7,3,66,3,-5,22,-77,2};
             int i;
             int j;
             int k;
             printf ("\n Unordered data:");
                 for (k=0; k<8;++k)
                     printf ("&#37;6d", a [k]);
                     printf ("\n");
                     for ( i=0; i<8 ; ++i)
                     for (j=i+1; j<8; ++j)
                        order (&a[i], &a[j]);
             printf ("After pass %d:", i+1);
                    for (k=0; k<8;k)
                    printf("%6d"' (k%2==0)?333:a[k]),
                    print ("\n");
    
            putchar ('\n');
            return 0;
    }
    void order (int *p, int *q)
    {
            int tmp;
            if (*p>*q)
          {
            tmp =*p;
           *p=*q;
           *q=tmp;
           }
    }
    Last edited by Dennery108; 08-12-2007 at 10:59 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what is SIZE?
    Code:
    #define order (int*p, int*q);
    this is not a function prototype. use this:
    Code:
    void order (int *p, int *q);
    example of properly formatted code:
    Code:
    #include <stdio.h>
    #define order (int*p, int*q);
    int main (void)
    {
        int a[]= {7,3,66,3,-5,22,-77,2};
        int i;
        int j;
        int k;
    
        printf ("\n Unordered data:");
    
        for (k=0; k<SIZE;++k)
            printf ("&#37;6d", a [k]);
        printf ("\n") /*where is the semicolon?*/
    
        for ( i=0; i<SIZE ; ++i)
            for (j=i+1; j<SIZE; ++j)
                order (&a[i], &a[j]);
    
        printf ("After pass %d:", i+1);
        for (k=0; k<SIZE;k)
            printf("%6d"' (k%2==0)?333:a[k]); /*don't you mean a comma?*/
        print ("\n");
    }/*where is this brace from?*/
        putchar ('\n');
        return 0;
    }
    void order (int *p, int *q)
    {
        int tmp;
        if (*p>*q)
        {
            tmp =*p;
            *p=*q;
            *q=tmp;
        }
    }
    Last edited by robwhit; 08-12-2007 at 10:00 PM.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    Robwhit,
    Thanks for the help.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    Well i got the errors out. but it was not what i wanted to do. I guess I have to start over again. Thanks for all the help.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what did you want it to do?

    what did it do?

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    I needed it to print this on the screen:

    Unordered data: 7,3,66,3,-5,22,-77,2
    after pass 1: -77,7,66,3,3,22,-5,2
    after pass 2: -77,-5,66,7,3,22,3,2


    But it printed something like this:

    333 333 333 333 333 333 333 333 333 333
    33 333 333 333 333 333 333 333 333 333

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Dennery, have you fixed the problems mentioned previously by robwhit? if so please post your complete updated code and we can check it out. thanks

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    Code:
    #include <stdio.h>
    void order (int *p, int *q);
    int main (void)
    {
        int a[]= {7,3,66,3,-5,22,-77,2};
        int i;
        int j;
        int k;
    
        printf ("\n Unordered data:");
    
        for (k=0; k<8;++k)
            printf ("&#37;6d", a [k]);
        	printf ("\n");
    
        for ( i=0; i<8 ; ++i)
            for (j=i+1; j<8; ++j)
                order (&a[i], &a[j]);
    
        printf ("After pass %d:", i+1);
        for (k=0; k<8;k)
            printf("%6d", (k%2==0)?333:a[k]);
        	printf ("\n");
    
        putchar ('\n');
        return 0;
    }
    void order (int *p, int *q)
    {
        int tmp;
        if (*p>*q)
        {
            tmp =*p;
            *p=*q;
            *q=tmp;
        }
    }
    Last edited by Dennery108; 08-13-2007 at 04:20 PM.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    printf("&#37;6d"' (k%2==0)?333:a[k]),
    You didn't fix this one correctly, the ' needs to be a comma, not the semicolon that you changed... which needs to go back to being a semicolon.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    Code:
    #include <stdio.h>
    void order (int *p, int *q);
    int main (void)
    {
        int a[]= {7,3,66,3,-5,22,-77,2};
        int i;
        int j;
        int k;
    
        printf ("\n Unordered data:");
    
        for (k=0; k<8;++k)
            printf ("%6d", a [k]);
        	printf ("\n");
    
        for ( i=0; i<8 ; ++i)
            for (j=i+1; j<8; ++j)
                order (&a[i], &a[j]);
    
        printf ("After pass %d:", i+1);
        for (k=0; k<8;k)
            printf("%6d", (k%2==0)?333:a[k]);
        	printf ("\n");
    
        putchar ('\n');
        return 0;
    }
    void order (int *p, int *q)
    {
        int tmp;
        if (*p>*q)
        {
            tmp =*p;
            *p=*q;
            *q=tmp;
        }
    }

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    #include <stdio.h>
    void order (int *p, int *q);
    int main (void)
    {
        int a[]= {7,3,66,3,-5,22,-77,2};
        int i;
        int j;
        int k;
    
        printf ("\n Unordered data:");
    
        for (k=0; k<8;++k)
            printf ("&#37;6d", a [k]);
        	printf ("\n");
    
        for ( i=0; i<8 ; ++i)
            for (j=i+1; j<8; ++j)
                order (&a[i], &a[j]);
    
        printf ("After pass %d:", i+1);
        for (k=0; k<8;k)//infinite loop--k is not modified at all
            printf("%6d", (k%2==0)?333:a[k]); //removing the weird if-else syntax and just printing a[k] seems to display the correct order
                                // why do you have to check to see if its even? and why are you printing 333 if it is?
        	printf ("\n");
    
        putchar ('\n');
        return 0;
    }
    void order (int *p, int *q)
    {
        int tmp;
        if (*p>*q)
        {
            tmp =*p;
            *p=*q;
            *q=tmp;
        }
    }

  15. #15
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    Someone trying to implement bubble sort with no knowledge of for loops or basic C operators? I think you're way ahead of yourself. Go back and learn some basic arrays, for loops, and C operators. Then try to implement a bubble sort. Make sure you can do it on paper first.

    Not trying to be mean, but that is the best way, judging by the errors I saw.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 12:27 PM
  2. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 11:34 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21