I would like to sort an array of floats using bubble sort in C. I would like this function to return this sorted array, and be assigned to my variable testSorted. However, when I print testSorted the output is 0.0000. Can anyone please help out here?
float * bubbleSort(float statsArray[], int n){
float swap;
for ( int a = 0; a < n-1; a++)
{
for (int b = 0; b < n - a - 1; b++)
{
if (statsArray[b] < statsArray[b+1])
{
swap = statsArray[b];
statsArray[b] = statsArray[b+1];
statsArray[b+1] = swap;
}
}
}
return statsArray;
}
void main() {
float test[SIZE] = { 34, 201, 190, 154, 8, 194, 2, 6,
114, 88, 45, 76, 123, 87, 25, 23,
200, 122, 150, 90, 92, 87, 177, 244,
201, 6, 12, 60, 8, 2, 5, 67,
7, 87, 250, 230, 99, 3, 100, 90};
/* Variable Declarations */
float average;
float * testSorted;
int p;
float maximum, minimum;
p = 40;
testSorted = bubbleSort(test, p);
printf(testSorted);
return 0;
}
printf(testSorted);
is quite broken, because the first argument of printf is supposed to be a pointer to a char *
format string, not an array of floats.
Instead, to print an array, you need to do something like the following:
printf("{ ");
for (int a = 0; a < SIZE; a++) {
printf("%f ", test[a]);
}
printf("}");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.