That post??? I tested it. What seems wrong?
Printable View
set n = sizeof(a) / sizeof(a[0] if you want to use sizeof().
It stinks though, because you may (and frequently will), make your array larger than you need, and just use part of it for your data.
Then sizeof() just sucks eggs, and you'll need n (some variable), to tell your program how many numbers, or strings or whatever, you need to have sorted, in your array. Sizeof() can never do that.
OK, off my soapbox, down to business:
1) If you're going to have the user input the values into a[], then remove the numbers now being put into a, by the program, at initialization.
2) set n to *something* that you want, that tells bubblesort what size the array is (or what number of elements it needs to sort).
3)Put the user input inside a while loop.
I've got to hit the sack for some zzzz's.
Good luck, Baretta.
=( )
ok apparently i am suppose to make the n a variable that hte user types in:Code:#include <stdio.h>
#include <stdlib.h>
/* The actual method of sorting */
void bubblesort(int a[],int n);
int main()
{
int n;
int list = 0;
int a[10] = { 4, 8, 2, 1, 6, 5, 3, 9, 8, 1};
printf("Please enter an integer: ");
scanf("%d", &n);
printf("Before: ");
while(list < sizeof(a) / sizeof(a[0]))
printf("%d", a[list++]);
printf("\n");
bubblesort(a, sizeof(a) / sizeof(int));
list = 0;
printf("After: ");
while(list < sizeof(a) / sizeof(a[0]))
printf("%d", a[list++]);
printf("\n");
system("pause");
return 0;
}
/*Defining the bubblesort*/
void bubblesort(int a[],int n)
{
int cmp, pass, tmp, swch;
pass = 0;
cmp = 0;
swch = 1;
while(swch)
{
swch = 0;
cmp = 1;
while(cmp < n - pass)
{
if(a[cmp-1] > a[cmp])
{
swch = 1;
tmp = a[cmp-1];
a[cmp-1] = a[cmp];
a[cmp] = tmp;
}
cmp++;
}
pass++;
}
printf("Our Sort had %d comparisons, and %d passes\n", cmp, pass);
}
- program asks for n
-then you type in the numbers you would like to sort
so if n = 6 there will be six numbers to sort.
any ideas, ill reply when i get back from work
Yea i understand what you are saying, i'm workin on it right now
only I haven't figured out why nothing is being output by the printf("%i, ", *ptr);Code:#include <stdio.h>
#include <stdlib.h>
int BubbleSort(int info, int next);//returns the lesser of the 2.
void main(void)
{
int *ptr = malloc(10 * sizeof (int));// will hold entire input array
int next=0;int info;
int i;
for (i=0;i<<*ptr;i++)
{
printf("Enter Number Please:");
scanf("%i",&info);
if (info<=next)
{
*ptr=info;
}
else *ptr=BubbleSort(info,next);
printf("%i, ",*ptr);
ptr++;
}
}
int BubbleSort(int info, int next)
{
int tempInfo;
tempInfo=info;
info=next;
next=tempInfo;
return info;
}
Am I close?
- Edward