为作业的Buble排序代码不按预期工作
我的任务是制作一个可以排序名称的程序。
程序的输出:
How many names will be inputed? 3
Angelia Kho
Angel
Angelina
Sorted Names(Ascending):
1. Angel
2. Angelia Kho
3. Angelina
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
int a;
char ignore;
printf("How Many names will be inputed? ");
scanf("%d", &a);
scanf("%c", &ignore);
char names [a][255];
char temp [255];
int j;
for(j = 0; j < a; j++){
scanf("%[^\n]", names[j]);
scanf("%c", &ignore);
}
int i;
for(i = 0; i<a; i++)
{
for(j = i; j<a; j++)
{
if(strcmp(names[i], names[j]> 0))
{
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j],temp);
}
}
}
printf("\nsorted names(Ascending): \n");
for(i = 0; i < a; i++)
{
printf("%d. %[^\n]", i, names[i]);
}
}
错误是:
[Warning]passing argument 2 of 'strcmp' makes pointer from integer without cast
但是,它仍然有效,直到
目前用户输入输出:
How Many names will be inputed? 3
Angelia Kho
Angel
Angelina
--------------------------------
Process exited after 23.08 seconds with return value 3221225477
Press any key to continue...
I have task to make a program that can sort names.
The output of the program:
How many names will be inputed? 3
Angelia Kho
Angel
Angelina
Sorted Names(Ascending):
1. Angel
2. Angelia Kho
3. Angelina
This is my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
int a;
char ignore;
printf("How Many names will be inputed? ");
scanf("%d", &a);
scanf("%c", &ignore);
char names [a][255];
char temp [255];
int j;
for(j = 0; j < a; j++){
scanf("%[^\n]", names[j]);
scanf("%c", &ignore);
}
int i;
for(i = 0; i<a; i++)
{
for(j = i; j<a; j++)
{
if(strcmp(names[i], names[j]> 0))
{
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j],temp);
}
}
}
printf("\nsorted names(Ascending): \n");
for(i = 0; i < a; i++)
{
printf("%d. %[^\n]", i, names[i]);
}
}
The error is:
[Warning]passing argument 2 of 'strcmp' makes pointer from integer without cast
But, it still working until user input
Output for now:
How Many names will be inputed? 3
Angelia Kho
Angel
Angelina
--------------------------------
Process exited after 23.08 seconds with return value 3221225477
Press any key to continue...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这样的初学者来说,
是多余的。您可以将它们删除,并更改SCANF以输入字符串的呼吁,以下面的方式
注意格式字符串中的领先空间。它允许跳过白空间字符,例如新的行字符
'\ n'
在先前的调用scanf
之后存储在输入缓冲区中。在if语句中
有一个错字。表达式
名称[J]&GT; 等于1
。
0
具有类型
int 不是气泡排序方法。它看起来像是选择排序的方法,并具有冗余掉期。
For starters calls like this
are redundant. You may remove them and change the call of scanf for entering strings the following way
Pay attention to the leading space in the format string. It allows to skip white space characters as for example the new line character
'\n'
stored in the input buffer after a preceding call ofscanf
.In the if statement
there is a typo. The expression
names[j] > 0
has the typeint
and is equal to 1.You have to write
Another typo in this call of
printf
Rewrote it like
Also the used method of sorting is not the bubble sort method. It looks like the selection sort method with redundant swaps.