simple sorting problem

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-12-2007
Dennery108
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.
• 08-12-2007
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)
• 08-12-2007
MacNilly
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```
• 08-12-2007
Dennery108
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;       } }```
• 08-12-2007
robwhit
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;     } }```
• 08-12-2007
Dennery108
Robwhit,
Thanks for the help.
• 08-12-2007
Dennery108
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.
• 08-12-2007
robwhit
what did you want it to do?

what did it do?
• 08-13-2007
Dennery108
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
• 08-13-2007
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
• 08-13-2007
Dennery108
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;     } }```
• 08-13-2007
hk_mp5kpdw
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.
• 08-13-2007
Dennery108
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;     } }```
• 08-13-2007
```#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;     } }```