[英]Bubble sorting an array in C in a separate function
我想在 C 中使用冒泡排序对浮点数组进行排序。 我希望这个 function 返回这个排序数组,并分配给我的变量 testSorted。 但是,当我打印 testSorted 时,output 是 0.0000。 有人可以在这里帮忙吗?
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);
很糟糕,因为 printf 的第一个参数应该是指向char *
格式字符串的指针,而不是浮点数组。
相反,要打印数组,您需要执行以下操作:
printf("{ ");
for (int a = 0; a < SIZE; a++) {
printf("%f ", test[a]);
}
printf("}");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.