1. 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!

2. 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[];`

3. 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.

4. 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().

5. well i used 4, then i added

scanf("%d", &n);

but when i do that nothing happens =\

6. Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
void bubblesort(int a[],int n);
int cmp, pass, tmp, n, a[4];
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?

7. 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.

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 =\

9. 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];
scanf("%d", &n);

system("pause");
return 0;
}```

10. 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;
scanf("%d", &n);

system("pause");
return 0;
}```

11. 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.

12. yes i did, its 1

13. No you didn't. Look again.

14. cmp = 1;
while(cmp <= n - pass)

15. I'm talking about the code I posted.