C 根据学生姓氏的字母顺序对数组排序
根据学生姓氏的字母顺序对数组进行排序,并将数组打印到控制台 但是代码没有正确显示输出,为什么?C 根据学生姓氏的字母顺序对数组排序,c,arrays,loops,sorting,C,Arrays,Loops,Sorting,根据学生姓氏的字母顺序对数组进行排序,并将数组打印到控制台 但是代码没有正确显示输出,为什么? #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> void main() { char name[10][8], temp[8]; int i,j,n,L,k; printf("Enter the value of number
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void main()
{
char name[10][8], temp[8];
int i,j,n,L,k;
printf("Enter the value of number:\n");
scanf("%d", &n);
fflush(stdin);
printf("\n");
printf("Please enter the name of the student:\n");
for(i=0;i<n;i++)
{
gets(name[i]);
}
for(i=0;i<n-1;i++)
{
k=0;
while(1)
{
++k;
if(name[i][k]==' ')
break;
}
for(j=i+1;j<n;j++)
{
L=0;
while(1)
{
++L;
if(name[j][L]==' ')
break;
}
if(name[i][k+1]>name[j][L+1])
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("After sorting the array:\n");
for(i=0;i<n;i++)
{
puts(name[i]);
}
return 0;
}
如果我提供意见。假设
输入:
Enter the value of number: 2
Please enter the name of the student:
Nihan ahmed
After sorting the array:
Nihan ahmed
输出:
Enter the value of number: 2
Please enter the name of the student:
Nihan ahmed
After sorting the array:
Nihan ahmed
为什么我不能输入多个名称?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void main()
{
char name[10][8], temp[8];
int i,j,n,L,k;
printf("Enter the value of number:\n");
scanf("%d", &n);
fflush(stdin);
printf("\n");
printf("Please enter the name of the student:\n");
for(i=0;i<n;i++)
{
gets(name[i]);
}
for(i=0;i<n-1;i++)
{
k=0;
while(1)
{
++k;
if(name[i][k]==' ')
break;
}
for(j=i+1;j<n;j++)
{
L=0;
while(1)
{
++L;
if(name[j][L]==' ')
break;
}
if(name[i][k+1]>name[j][L+1])
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("After sorting the array:\n");
for(i=0;i<n;i++)
{
puts(name[i]);
}
return 0;
}
#包括
#包括
#包括
#包括
void main()
{
字符名[10][8],临时值[8];
int i,j,n,L,k;
printf(“输入数字:\n的值”);
scanf(“%d”和“&n”);
fflush(stdin);
printf(“\n”);
printf(“请输入学生姓名:\n”);
对于(i=0;i,根据该循环,您正在读取n次:
for(i=0;i<n;i++)
{
gets(name[i]);
}
for(i=0;i这里:get(name[i]);
对于从用户获取输入有一些限制。
我建议使用scanf(“%s”,name[I])
。
在你的代码中,如果你在
for(i=0;i<n;i++)
{
printf("%d \n", i);
gets(name[i]);
}
OUTPUT:
Enter the value of number:
2
Please enter the name of the student:
0
1
hello
After sorting the array:
hello
for(i=0;i(等待输入)->1->(等待输入)
如果您使用scanf来代替,代码将按预期工作。
请使用以下命令更改gets循环的代码:
for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}
OUTPUT:
Enter the value of number:
2
Please enter the name of the student:
hello
abcd
After sorting the array:
hello
abcd
(i=0;i)的