gpt4 book ai didi

c - 使用 2 函数进行冒泡排序

转载 作者:行者123 更新时间:2023-11-30 14:46:52 26 4
gpt4 key购买 nike

起初我使用冒泡排序对一些数字进行排序。它起作用了。然后我再次尝试使用具有相同逻辑的 2 个函数的冒泡排序进行排序。但它不起作用。我是使用 2/3 函数的新手.那么谁能帮我找出我的逻辑问题吗?

First source

#include<stdio.h>
main()
{
int i,j;
float num[5], c;

for(i=0;i<5;i++)
{
scanf("%f",&num[i]);
}
for(j=0;j<4;j++)
{
for(i=0;i<5;i++)
{
if(num[i]<num[i+1])
{
c=num[i];
num[i]=num[i+1];
num[i+1]=c;
}
}
}
for(i=0;i<5;i++)
{
printf("%.2f\n",num[i]);
}
}

Second source w/function

#include<stdio.h>
void sort(int a[]);
void main()
{
int i;
double a[3], A, B, C;

for(i=0;i<3;i++)
{
scanf("%lf",&a[i]);
}

sort(a);

printf("In The Function\n");
for(i=0;i<3;i++)
{
printf("%.2lf\n",a[i]);
}

return;
}

void sort(int a[])
{
int i, n;
double temp;

for(n=0;n<2;n++)
{
for(i=0;i<3;i++)
{
if(a[i]<a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
return;
}

最佳答案

不要忽略警告。

warning: passing argument 1 of ‘sort’ from incompatible pointer type [enabled by default]
sort(a);
^
sort.c:2:6: note: expected ‘int *’ but argument is of type ‘double *’
void sort(int a[]);
^

您已将 a 声明为 double,但您的 function 接收 a 作为 int >。将其更改为如下。

void sort(double a[]);

关于c - 使用 2 函数进行冒泡排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51906232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com