1. *slaps head* i look in wrong area, i understand what you are saying

ok now how do i call bubblesort into my actual program?

2. No. Code is executed line by line, in order. In other words if I have:
Code:
```int i;
if(i == 5)
printf("i is equal to 5\n");
i = 5;```
In the above code, the check if i is equal to five happens before i is set to 5.

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

4. 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;
}```

5. hmm, i'm a bit lost honestly.

6. Here is your code with a few changes to the main function so it will run:
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;
}```
Now compile and run that code. What happens?

7. 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.
//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 */```

8. 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;
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;
}
}```
thank you for the arrangement help
does that look any better?

it compiles and i can enter n, but it does nothing afterwards

9. hmm i see i forgot the add an output that would state the array. hmm

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

11. does that look any better?
It looks better, but your indentation still needs some help. Look at the code I posted; it is properly indented.

12. 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;
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;
}```
it's working and I fixed the indentions for you sir =)

13. it's working!
No it's not. Your code isn't sorting anything. Bubble sorts require 2 loops, you just have one now...

14. well i meant that some code is actually showing. lol.

hmmmmmm

15. 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;
}```