-
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.
-
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)
-
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 ("%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
-
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 ("%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;
}
}
-
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 ("%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;
}
}
-
Robwhit,
Thanks for the help.
-
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.
-
what did you want it to do?
what did it do?
-
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
-
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
-
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;
}
}
-
Code:
printf("%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.
-
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;
}
}
-
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)//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;
}
}
-
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.
-
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.
Get rid of all the functions and other crap you've got going on that are wrong and just use a main function with one array, and a two loops and a simple swap (no function calling swap). If you can do that, and get it to work, add one small thing to it, TEST IT, if it works, add another, repeat. Modularize code that has been testing into functions. This is called programming. You can't have 100 things going on that have errors and expect to correct it. Start from the very basics and build up, slowly, testing everything.
If you got this far, I'll give you a hint.
Make a function called printarray that takes 2 parameters a pointer to the type of elements in your array, in this case, integers, and a number of elements. Use a for loop to print each value in the array to the screen in a readable format (your gonna be reading this stuff not anyone else). Then instead of cluttering up your code with a thousand for loops, just call printarray(thisarray, thisarraysize). Makes it easier on ya man.