# Help understanding bubblesort hw?

Show 80 post(s) from this thread on one page
Page 1 of 5 12345 Last
• 04-19-2009
beretta
Help understanding bubblesort hw?
Code:

```#include <stdio.h> #include <stdlib.h>   void bubblesort(int a[],int n);     int main() {         int comp, pass, tmp, n;     int a[];     pass = 1;     while(comp <= n - pass)     {         if(a[comp-1] > a[comp])         {             tmp = a[comp-1];             a[comp-1] = a[comp];             a[comp] = tmp;         }         comp = comp + 1;     }     pass = pass + 1;     system("pause");     return 0;  }```
is what i have so far and it tells me that:
array size missing in 'a'

also im having trouble understanding the homework

Write two functions, both accepts two arguments, an integer array: a and an integer: n. The first function outputs the n elements stored

in the array, and the second function sorted the array using the basic bubble sort as provided in the notes with an improvement. The

improvement is based on the following observation. When there is no exchange between any data in a pass of the bubble sort, the sequence

is already sorted in non-decreasing order. As a result, there is no need to continue and hence can terminate the sort. Add an output

statement to show the number of passes that the sorting algorithm has actually taken.

Then write a C program to test your solution. It first requests n, the number of integers to be sorted. Then it input n integers. Then

it calls the output function to output the original sequence of integers, then calls the bubble sort, and finally calls the output

function to output the final sorted sequence.

can anyone explain this more clearly for me?

thank you!
• 04-19-2009
MK27
Quote:

Originally Posted by beretta
is what i have so far and it tells me that:
array size missing in 'a'

hmmm....could there be something obvious you have failed to investigate?!!??!
Code:

`int a[];`
• 04-19-2009
beretta
well i tried it without that string, and it still gives me the same error sir. the point is that i don't know what size the array will be, im going to have to add to the program an input.
• 04-19-2009
MK27
Quote:

Originally Posted by beretta
the point is that i don't know what size the array will be.

Yep. So pick something, or allocate dynamically with malloc().
• 04-19-2009
beretta
well i used 4, then i added

printf("Please enter an integer: ");
scanf("%d", &n);

but when i do that nothing happens =\
• 04-19-2009
beretta
Code:

```#include <stdio.h> #include <stdlib.h>     int main() {   void bubblesort(int a[],int n);     int cmp, pass, tmp, n, a[4];     printf("Please enter an integer: ");     scanf("%d", &n);     {       pass = 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;       }     }     return 0;  }```
have that so far. does it look like im in the right direction?
• 04-19-2009
You'll want this part of your code in a loop will you not?

Code:

```    printf("Please enter an integer: ");     scanf("%d", &n);```
May I suggest you use a #define Size 4, and then use a for loop from 0 to < Size to fill the array?

Also, you declared a sort function, but so far, haven't created it, or called it.
• 04-19-2009
beretta

and i thought n would only be called initially.

i also wasn't instructed or taught on using #define size 4, we have to use it as given =\
• 04-20-2009
beretta
Ok, i think i understand what you mean, first i need to set up the sort as in define it. then i can call it in the actual program, so like this?

Code:

```#include <stdio.h> #include <stdlib.h>   void bubblesort(int a[],int n)     {       int cmp, pass, tmp;       pass = 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;       }     }    int main() {         int cmp, pass, tmp, n, a[4];     printf("Please enter an integer: ");     scanf("%d", &n);            system("pause");     return 0;  }```
• 04-20-2009
beretta
I think this is even a "neater" version, but tell me if i'm wrong.

Code:

```/*Defining the bubblesort*/ void bubblesort(int a[],int n)     {       int cmp, pass, tmp;       pass = 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;       }     }    /* The actual method of sorting */ #include <stdio.h> #include <stdlib.h> void bubblesort(int a[],int n); int main() {     int a[100];     int cmp, pass, tmp, n;     printf("Please enter an integer: ");     scanf("%d", &n);             system("pause");     return 0;  }```
• 04-20-2009
bithub
Code:

```      int cmp, pass, tmp;       pass = 1;       while(cmp <= n)```
Look at these 3 lines of code. On the third line, you are checking if cmp is less than or equal to n. You have never assigned any value to cmp though.
• 04-20-2009
beretta
yes i did, its 1
• 04-20-2009
bithub
No you didn't. Look again.
• 04-20-2009
beretta
cmp = 1;
while(cmp <= n - pass)
• 04-20-2009
bithub
I'm talking about the code I posted.
Show 80 post(s) from this thread on one page
Page 1 of 5 12345 Last