*slaps head* i look in wrong area, i understand what you are saying
ok now how do i call bubblesort into my actual program?
*slaps head* i look in wrong area, i understand what you are saying
ok now how do i call bubblesort into my actual program?
Last edited by beretta; 04-20-2009 at 12:37 AM.
No. Code is executed line by line, in order. In other words if I have:
In the above code, the check if i is equal to five happens before i is set to 5.Code:int i; if(i == 5) printf("i is equal to 5\n"); i = 5;
yea i understand, i apologize about that.
so if i added cmp = 1;
does the sort look valid?
if so how do i actually use it in the program
Your code is still wrong. The bubblesort() function will never return; it will loop forever. Look at your loop conditions. The outer loop will run forever because cmp will always be less than or equal to n.
As for how to use your program, here is one method:
Code:int main(void) { int a[3] = { 3, 2, 1 }; bubblesort(a, sizeof(a) / sizeof(int)); return 0; }
hmm, i'm a bit lost honestly.
Here is your code with a few changes to the main function so it will run:
Now compile and run that code. What happens?Code:#include <stdio.h> #include <stdlib.h> void bubblesort(int a[],int n) { int cmp = 1, pass, tmp; pass = 1; while(cmp <= n) { printf("Inside outer loop. cmp=%d, n=%d\n",cmp,n); cmp = 1; while(cmp <= n - pass) { printf("Inside inner loop. cmp=%d, n-pass=%d\n",cmp,n-pass); if(a[cmp-1] > a[cmp]) { printf("Swapping variables\n"); tmp = a[cmp-1]; a[cmp-1] = a[cmp]; a[cmp] = tmp; } cmp = cmp + 1; } pass = pass + 1; } } int main(void) { int a[3] = { 3, 2, 1 }; bubblesort(a, sizeof(a) / sizeof(int)); return 0; }
I liked where you where with this code, Baretta. I've taken the liberty of just re-arranging it so it's "right side up" (has the includes at the top of the program).
Code:#include <stdio.h> #include <stdlib.h> void bubblesort(int a[],int n); int main() { int a[10] = { 3, 1, 7, 6, 2, 8, 9, 5, 4, 0 }; int cmp, pass, tmp, n; //we can remove this line of code, since it belongs in the bubblesort() function. //printf("Please enter an integer: "); //scanf("%d", &n); system("pause"); return 0; } /*Defining the bubblesort*/ void bubblesort(int a[],int n) { int cmp, pass, tmp; pass = cmp = 0; <-- We haven't made any passes or comparisons, yet while(cmp < n) <-- Just less than n, not equal to, this is C { cmp = 1; <- instead of =1, let's increment it by one: cmp++; while(cmp <= n - pass) <-- looks correct { if(a[cmp-1] > a[cmp]) { tmp = a[cmp-1]; a[cmp-1] = a[cmp]; a[cmp] = tmp; } cmp = cmp + 1; } pass = pass + 1; } } /* The actual method of sorting */
Last edited by Adak; 04-20-2009 at 01:19 AM.
thank you for the arrangement helpCode:#include <stdio.h> #include <stdlib.h> /* The actual method of sorting */ void bubblesort(int a[],int n); int main() { int a[4]; int n; printf("Please enter an integer: "); scanf("%d", &n); bubblesort(a, sizeof(a) / sizeof(int)); system("pause"); return 0; } /*Defining the bubblesort*/ void bubblesort(int a[],int n) { int cmp, pass, tmp; pass = 1; cmp = 1; while(cmp <= n) { cmp = 1; while(cmp <= n - pass) { if(a[cmp-1] > a[cmp]) { tmp = a[cmp-1]; a[cmp-1] = a[cmp]; a[cmp] = tmp; } cmp = cmp + 1; } pass = pass + 1; } }
does that look any better?
it compiles and i can enter n, but it does nothing afterwards
hmm i see i forgot the add an output that would state the array. hmm
It's hard to believe, but when you are designing a program, in the first half of the design process, ignore the damn details.
Whether the program works is at this time, a mere detail. Welcome to top-down design!
Did you make the other changes in the code as I marked them out, previously?
*bithub: It's after Midnight, and you're asking a young person to waste sleep to prove that his code will loop endlessly, as you stated.
That seems unnecessary at the very least, at this hour.
Last edited by Adak; 04-20-2009 at 01:26 AM.
It looks better, but your indentation still needs some help. Look at the code I posted; it is properly indented.does that look any better?
it's working and I fixed the indentions for you sir =)Code:#include <stdio.h> #include <stdlib.h> /* The actual method of sorting */ void bubblesort(int a[],int n); int main() { int a[4]; int n; printf("Please enter an integer: "); scanf("%d", &n); bubblesort(a, sizeof(a) / sizeof(int)); system("pause"); return 0; } /*Defining the bubblesort*/ void bubblesort(int a[],int n) { int cmp, pass, tmp; pass = 1; cmp = 1; printf("Inside outer loop. cmp=%d, n=%d\n",cmp,n); while(cmp <= n - pass) { printf("Inside inner loop. cmp=%d, n-pass=%d\n",cmp,n-pass); if(a[cmp-1] > a[cmp]) { tmp = a[cmp-1]; a[cmp-1] = a[cmp]; a[cmp] = tmp; } cmp = cmp + 1; } pass = pass + 1; }
Last edited by beretta; 04-20-2009 at 01:27 AM.
No it's not. Your code isn't sorting anything. Bubble sorts require 2 loops, you just have one now...it's working!
well i meant that some code is actually showing. lol.
hmmmmmm
You still have a problem in your main function. You are not assigning any values to the array you are sorting. Put some values in there!
Code:int main(void) { int i; int a[4] = {4, 8, 2, 1}; printf("Before calling bubblesort()...\n"); for(i = 0; i < sizeof(a) / sizeof(a[0]); ++i) printf("%d ", a[i]); printf("\n"); bubblesort(a, sizeof(a) / sizeof(int)); printf("After calling bubblesort()...\n"); for(i = 0; i < sizeof(a) / sizeof(a[0]); ++i) printf("%d ", a[i]); printf("\n"); system("pause"); return 0; }