C 理解数组和函数
我给你看的程序询问用户人数,并询问每个人吃了多少煎饼。之后,它会打印出最多数量的煎饼。 好吧,直到现在我发现这很容易做到。 我想做的是(不使用指针)说出吃得最多的人 以下是我到目前为止所做的C 理解数组和函数,c,arrays,C,Arrays,我给你看的程序询问用户人数,并询问每个人吃了多少煎饼。之后,它会打印出最多数量的煎饼。 好吧,直到现在我发现这很容易做到。 我想做的是(不使用指针)说出吃得最多的人 以下是我到目前为止所做的 #include <stdio.h> int main() { int person, i; int pancake[50]; printf("Enter Number Of People: "); scanf("%d", &person); for (i=1; i<=per
#include <stdio.h>
int main()
{
int person, i;
int pancake[50];
printf("Enter Number Of People: ");
scanf("%d", &person);
for (i=1; i<=person; i++)
{
printf("Person [%d]: ", i);
scanf("%d",&pancake[i]);
}
for (i=1; i<=person; i++)
{
if (pancake[1]<pancake[i])
{
pancake[1] = pancake[i];
}
}
printf("Most pancakes eaten is %d\n", pancake[1]);
}
#包括
int main()
{
国际人士,我;
国际煎饼[50];
printf(“输入人数:”);
scanf(“%d”和人);
对于(i=1;i无需使用指针。由于代码中存在许多错误/打字错误,我正在发布完整的modified1.代码
#include <stdio.h>
#include <string.h>
int main(void)
{
int person, i;
int pancake[50];
printf("Enter Number Of People: ");
scanf("%d", &person);
char name[person][30]; // 2D array to store the name of persons. Note that I used variable length arrays.
for (i=0; i < person; i++)
{
printf("Person [%d]: ", i+1);
scanf("%d",&pancake[i]);
printf("Person %d name: ", i+1);
getchar(); // To eat up the newline left behind by previous scanf.
fgets(name[i], 30, stdin); // To read the persons name. I removed the scanf.
}
for (i=0; i<person-1; i++)
{
if (pancake[0]<pancake[i+1])
{
pancake[0] = pancake[i+1];
strcpy(name[0] , name[i+1]); // A function in <string.h> to copy strings.
}
}
printf("Most pancakes eaten is %d by %s \n", pancake[0], name[0]);
}
#包括
#包括
内部主(空)
{
国际人士,我;
国际煎饼[50];
printf(“输入人数:”);
scanf(“%d”和人);
char name[person][30];//用于存储人名的2D数组。请注意,我使用了可变长度数组。
对于(i=0;i 对于@haccks解决方案的(i=0;i修改版,该解决方案演示了如何以高效的方式不使用指针。请注意,将幻数省略到#defines中,插入返回以便编译
#include <stdio.h>
#include <string.h>
#declare NUM_PANCAKES 50
#declare NAME_LENGTH_MAX 30
int main(void)
{
int person, i;
int pancake[NUM_PANCAKES];
int best_index = 0;
int most_pancakes = 0;
printf( "Enter Number Of People: ");
scanf( "%d", &person);
char name[person][NAME_LENGTH_MAX]; /* variable length list... */
/* get the data */
for ( i = 0; i < person; i++ )
{
printf( "Person [%d]: ", i + 1);
scanf( "%d", &pancake[i] );
printf( "Person %d name: ", i + 1 );
getchar();
fgets( name[i], NAME_LENGTH_MAX, stdin );
}
/* find the answer */
for ( i = 0; i < person - 1; i++ )
{
if ( pancake[i] > most_pancakes )
{
most_pancackes = pancake[i];
best_index = i;
}
}
printf( "Most pancakes eaten is %d by %s \n",
pancake[best_index],
name[best_index] );
return 0;
}
#包括
#包括
#申报煎饼50个
#声明名称\u长度\u最大值30
内部主(空)
{
国际人士,我;
int煎饼[NUM_煎饼];
int最佳指数=0;
int most_煎饼=0;
printf(“输入人数:”);
scanf(“%d”和人);
字符名[个人][姓名_长度_最大值];/*可变长度列表*/
/*获取数据*/
对于(i=0;i大多数煎饼)
{
大多数煎饼=煎饼[i];
最佳指数=i;
}
}
printf(“大多数煎饼是%s吃的%d\n”,
煎饼[最佳指数],
名称[最佳索引];
返回0;
}
小字体。煎饼
应该是煎饼
。谢谢:)刚刚更正了第一课:数组索引从0
开始。数组长度是第一个非法索引。因此,让循环从小于数组长度的0到1运行,您应该使用良好的C风格使用i
进行测试。请注意,C中的数组从0开始。pancakes[50]的最后一个元素
是煎饼[49]
。您实际上是在数组之外进行写入(典型的缓冲区溢出)哦,好吧,我的不好,我会记住这一点,谢谢你。我猜如果你对他或她的代码所做的更改发表评论,这会对OP有所帮助。OP似乎很难理解C中的数组是如何工作的。我不确定添加名称是否是一个好主意,这会导致字符串缓冲区溢出和“scanf-vs- fgets`蠕虫,可能会更混淆OP。@MOehm;好的。用一些有用的链接编辑了它:)。是的,看起来不错。:-(旁白:省略getchar()
而使用%d”
格式(带尾随空格)不是更好吗在fscanf
?您无法确定下一个字符是换行符,例如,如果用户输入“42\n”
)与其在第二位复制和覆盖你的数据,你可以在第二位找到吃得最多的数据。你可以只获取拥有最多的索引并使用它…如果他想找到下一个其他的东西怎么办?@haccks:除了你,没有人谈论指针。UpAndAdam建议为p最多的人保留一个整数索引你最后的评论对我来说毫无意义。