Hey There. I'm pretty new to C and I was trying to write this sort program. It will display the first line nicely, but it won't display lines pass 1 through pass 7. Can anyone see any errors with anything? should I have the call in the for loop.
Please help.
Code:
#include <stdio.h>
#define SIZE 8
void swap(int*,int*);
void sort(int a[], int n);
int main(void)
{
int i,cnt=0,a[SIZE] = {7,3,66,3,-5,22,-77,2};
printf("Unsorted Data:");
for(i=0; i<SIZE; i++){
printf(" %i", a[i]);
}
putchar('\n');
sort(a,SIZE);
for(i=0; i<SIZE; i++){
++cnt;
printf("After Pass: %d", cnt);
printf(" %i", a[i]);
}
putchar('\n');
return 0;
}
void sort(int a[], int n)
{
int i,j;
for(i=0; i<SIZE; ++i)
for (j = i + 1; j<SIZE; ++j)
if (a[i] > a[j])
swap(&a[i], &a[j]);
}
void swap(int *p, int *q)
{
int tmp;
tmp = *q;
*p = *q;
*q = tmp;
}