[英]Bubble sorting an array in C in a separate function
I would like to sort an array of floats using bubble sort in C.我想在 C 中使用冒泡排序对浮点数组进行排序。 I would like this function to return this sorted array, and be assigned to my variable testSorted.
我希望这个 function 返回这个排序数组,并分配给我的变量 testSorted。 However, when I print testSorted the output is 0.0000.
但是,当我打印 testSorted 时,output 是 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.很糟糕,因为 printf 的第一个参数应该是指向
char *
格式字符串的指针,而不是浮点数组。
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("}");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.