【发布时间】:2016-08-26 07:46:19
【问题描述】:
嘿,我不明白为什么我在将元素作为 input 时需要两个输入。我在 TurboC 编译器 GCC 上尝试了这段代码,但得到了同样的错误。
#include <stdio.h>
int menu();
void bubble_short();
void selection_short();
int main()
{
int ch,j,n,a[100];
ch=menu();
switch (ch)
{
case 1:
{
bubble_short();
break;
}
case 2:
{
selection_short();
}
default :
break;
}
}
void bubble_short()
{
int i,j,n,a[100];
printf("Elements");
scanf("%d",&n);
for (j=0; j<n;j++)
{
scanf("%d",&a[j]);
}
for (i=0;i<n;i++)
{
for (j=0;j<n-1-i;j++)
{
if (a[j]>a[j+1])
{
a[j]=a[j]+a[j+1];
a[j+1]=a[j]-a[j+1];
a[j]=a[j]-a[j+1];
}
}
}
printf("the sorted elements are :\n");
for ( i = 0; i < n; i++)
{
printf("%d\n",a[i]);
}
}
void selection_short()
{
int i,j,n,a[100],min;
printf("Elements");
scanf("%d",&n);
for ( i = 0; i <n-1; ++i)
{
min=i;
for ( j = 1+i; i < n; ++i)
{
if(a[min]>a[j])
min=j;
}
if(i!=min)
{
a[i]=a[i]+a[min];
a[min]=a[i]-a[min];;
a[i]=a[i]-a[min];;
}
}
printf("the shorted elements are :\n");
for ( i = 0; i < n; ++i)
{
printf("%d\n",a[i] );
}
}
int menu()
{
int k;
printf("Enter the choice \n 1. bubble short \n 2. selectionshort");
scanf("\n %d ",&k);
return k;
}
嘿,我不明白为什么我在将元素作为输入时需要两个输入。我在 TurboC 编译器 GCC 上尝试了这段代码,但得到了同样的错误。
输出
【问题讨论】:
-
void bubble_short(int n)-->void bubble_short(void) -
那是什么错误?据您解释,您两次收到相同的错误。要我们自己去你家看看吗?把它放在这里让我们读一下不是更容易吗?
-
我在这里附上我的 C 文件。我不知道为什么 scanf 比 printf 之前运行。我在这里附上完整的代码。执行后它应该只接受一个输入,它需要 2,最后一个是第二个 scanf 。
-
@LuisColorado 请立即检查我的输出中的错误,并附上代码。为什么我必须在输出上写的元素上方输入两个输入。
标签: c