Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中带零的冒泡排序_C_Bubble Sort - Fatal编程技术网

C中带零的冒泡排序

C中带零的冒泡排序,c,bubble-sort,C,Bubble Sort,所以,我第一次在C中尝试冒泡排序,我的代码可以正常工作,但如果我输入0作为输入,排序会使其他所有内容都为0。我不知道我的代码出了什么问题。多谢各位 /* Double-Click To Select Code */ #include<stdio.h> void main() { int w,f,temp,j; float arr[25]; printf("Enter the number of elements in the Array: "); scanf("%d"

所以,我第一次在C中尝试冒泡排序,我的代码可以正常工作,但如果我输入0作为输入,排序会使其他所有内容都为0。我不知道我的代码出了什么问题。多谢各位

/* Double-Click To Select Code */


#include<stdio.h>

void main()
{
 int w,f,temp,j;
 float arr[25];

 printf("Enter the number of elements in the Array: ");
 scanf("%d",&f);
 printf("\nEnter the elements:\n\n");

 for(w=0 ; w<f ; w++)
 {
  printf(" Array[%d] = ",w);
  scanf("%f",&arr[w]);
 }


for(w=0 ; w<f ; w++)
{
    for(j=0 ; j<f-w-1 ; j++)
    {
    if(arr[j]>arr[j+1]) //Swapping Condition is Checked
        {
        temp=arr[j];
        arr[j]=arr[j+1];
        arr[j+1]=temp;
        }
    }
}
printf("\nThe Sorted Array is:\n\n");
for(w=0 ; w<f ; w++)
    {
    printf(" %4f",arr[w]);
    }
}
/*双击以选择代码*/
#包括
void main()
{
内部w,f,温度,j;
浮动arr[25];
printf(“输入数组中的元素数:”);
scanf(“%d”、&f);
printf(“\n输入元素:\n\n”);

对于(w=0;wI)测试。只需将温度类型更改为浮动。这是有效的

#include<stdio.h>

void main()
{
     int w,f,j;
     float arr[25],temp;

     printf("Enter the number of elements in the Array: ");
     scanf("%d",&f);
     printf("\nEnter the elements:\n\n");

     for(w=0 ; w<f ; w++)
     {
      printf(" Array[%d] = ",w);
      scanf("%f",&arr[w]);
     }


    for(w=0 ; w<f ; w++)
    {
        for(j=0 ; j<f-w-1 ; j++)
        {
        if(arr[j]>arr[j+1]) //Swapping Condition is Checked
            {
            temp=arr[j];
            arr[j]=arr[j+1];
            arr[j+1]=temp;
            }
        }
    }
    printf("\nThe Sorted Array is:\n\n");
    for(w=0 ; w<f ; w++)
        {
        printf(" %4f",arr[w]);
        }
    system("pause");
}
#包括
void main()
{
int w,f,j;
浮子arr[25],温度;
printf(“输入数组中的元素数:”);
scanf(“%d”、&f);
printf(“\n输入元素:\n\n”);

对于(w=0;w假设您正在尝试对浮点进行排序,则需要将temp设置为浮点:

int w,f,j;
float arr[25];
float temp;

int类型的temp变量截断浮点值。将其更改为float。在输出0.000 0.000 1.000中,第一个或第二个零为0.33
截断为零,第三个值为is

temp
应为
float
,而不是
int
。排序良好,当我输入0作为输入时没有问题?temp应为float,当您使用25作为输入int arr的最大数量时,则强制用户也不要输入超过25,否则将损坏堆栈。排序没有问题。void main()已过时。请使用int main()并返回0。使用float temp而不是int temp。我已将temp更改为float,效果良好。非常感谢