When swch stays with a value of zero, then the sorting will stop.
When swch stays with a value of zero, then the sorting will stop.
that doesn't work cause i can type in 6 and get a negative number sir
[edit] WHOOPS! I just noticed there's 4 pages here, not 2! (I though the last post was hours ago)...anyway you might find this handy:
That's a modified bubblesort; it uses a flag:Originally Posted by your prof
Code:#include "stdio.h" void bubblesort (int *ray, int len) { int i, flag=0, tmp; while (flag==0) { for (i=0;i<len-1;i++) { /* loop thru all the elements, first to SECOND last */ if (ray[i]>ray[i+1]) { /* (since we must compare to the next element) */ tmp=ray[i+1]; ray[i+1]=ray[i]; ray[i]=tmp; flag++; /* a change made, flag goes up */ } } if (flag==0) break; /* no changes made! while loop is finished */ flag=0; /* reset flag */ } } int main () { int ray[11]={666,10,6,34,5,16,102,45,7,9,1}, i; bubblesort(ray,11); /* verify */ for (i=0;i<11;i++) printf("%d\n",ray[i]); return 0; }
Last edited by MK27; 04-20-2009 at 03:08 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
You're going to tell me what negative number you're talking about, right?
I was worried about your sleep after Midnight, now I'm worried about mine!
i've never heard of a flag, my teacher is an idiot.
if i use that code and type in the number 6Code:#include <stdio.h> #include <stdlib.h> /* The actual method of sorting */ void bubblesort(int a[],int n); int main() { int list = 0; int a[4] = { 4, 8, 2, 1}; printf("Before: "); while(list < sizeof(a) / sizeof(a[0])) printf("%d", a[list++]); printf("\n"); bubblesort(a, sizeof(a) / sizeof(int)); list = 0; printf("After: "); while(list < sizeof(a) / sizeof(a[0])) printf("%d", a[list++]); printf("\n"); system("pause"); return 0; } /*Defining the bubblesort*/ void bubblesort(int a[],int n) { printf("Please enter an integer: "); scanf("%d", &n); int cmp, pass, tmp, swch; pass = 0; cmp = 0; swch = 1; while(swch) { swch = 0; cmp = 1; while(cmp < n - pass) { if(a[cmp-1] > a[cmp]) { swch = 1; tmp = a[cmp-1]; a[cmp-1] = a[cmp]; a[cmp] = tmp; } cmp++; } pass++; } printf("Our Sort had %d comparisons, and %d passes\n", cmp, pass); }
i get the answer of -1124
Yeah, sorry -- I honestly didn't notice the rest of the thread and thought you'd gotten stuck. Don't get too distracted by my little contribution
The reason you're not using a flag is you're not using two loops in the function, you're using a control loop for the function, which should accomplish the same thing, so that's okay. Lemme take a look at your last post...
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
yea, and honestly im not even sure if i should be using:
while(list < sizeof(a) / sizeof(a[0])) or bubblesort(a, sizeof(a) / sizeof(int));
meaning the sizeof parts
You don't need sizeof, anything.
If you were malloc'ing your own memory dynamically, then you would need sizeof().
Looks mostly fine to me...
Well, you are suppose to be inputing all the data anyway, so that will determine the number you are calculating now with sizeof.
The bubblesort is done, now you just have to organize the rest of it. Move this into main:
The last place that should be is in the sort function. You want to get all the user input *before* anything else happens.Code:printf("Please enter an integer: "); scanf("%d", &n);
Then just turn your while/printf loop
into an actual function, as per the prof's specs.Code:while(list < sizeof(a) / sizeof(a[0])) printf("%d", a[list++]);
w/r/t the user input have you/are you supposed to use malloc() to create a dynamically sized array?
ps. you are using a flag -- you call it "swch". I thought of it that way too (as something that's switched on as a signal). The term "flag" prevents confusion with the "switch/case" construct which is used in C and most other languages.
Last edited by MK27; 04-20-2009 at 03:31 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
MK, how is sizeof() going to help sort an array of 100 elements, which might hold only 20 numbers in it?
Since it's not dynamically allocated, why emphasize sizeof(), when sizeof() won't help in the above case, which is common?
what about this version. can you fix this one?Code:#include <stdio.h> #include <stdlib.h> /* The actual method of sorting */ void bubblesort(int a[],int n); int main() { int n; int list = 0; int a[4] = { 4, 8, 2, 1}; printf("Please enter an integer: "); scanf("%d", &n); printf("Before: "); while(list < sizeof(a) / sizeof(a[0])) printf("%d", a[list++]); printf("\n"); bubblesort(a, sizeof(a) / sizeof(int)); list = 0; printf("After: "); while(list < sizeof(a) / sizeof(a[0])) printf("%d", a[list++]); printf("\n"); system("pause"); return 0; } /*Defining the bubblesort*/ void bubblesort(int a[],int n) { int cmp, pass, tmp, end; pass = 1; while(pass <= n-1) { cmp = 0; end = n - 1 - pass; while(cmp <= end) { if(a[cmp] > a[cmp+1]) { tmp = a[cmp]; a[cmp] = a[cmp+1]; a[cmp+1] = tmp; cmp++; } pass++; } } printf("Our Sort had %d comparisons, and %d passes\n", cmp, pass); }
No. It makes no sense in main().
sizeof(a) / sizeof(a[0]) will not tell you how many elements are being used in the array, so it's no good for this.
Last edited by Adak; 04-20-2009 at 03:52 AM.
how can you make sense of it?
I think the sizeof issue is a red herring at this point.
Here's a way to input the data. If you aren't using malloc(), then you need to set the array bigger than the user could possibly want it; n will act as a control with the functions, so the extra elements won't be involved.
Just combine that with your bubblesort. This does not verify the input -- it presumes the user is able to properly enter the numbers.Code:#include <stdio.h> #include <stdlib.h> void showarray (int *rayptr, int len) { int i=0; while (i<len) { printf("%d ",rayptr[i]); i++; } printf("\n"); } int main() { int n, a[100], i=0; /* presume we won't need more than one hundred numbers */ printf("Enter an array size: "); scanf("%d",&n); printf("Enter %d numbers seperated by spaces: ",n); while (i<n) { scanf("%d",&a[i]); i++; } /* verify using a showarray function */ showarray(a,n); return 0; }
Last edited by MK27; 04-20-2009 at 04:03 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
didn't work for me