gpt4 book ai didi

c - 理解数组和函数

转载 作者:行者123 更新时间:2023-11-30 19:47:33 25 4
gpt4 key购买 nike

我向您展示的程序要求用户提供“否”。的人,并询问每个人吃了多少煎饼。然后它打印出最大数量的煎饼。好吧,到目前为止我发现这很容易做到。我想做的是(不使用指针)指出吃得最多的人。

这是我到目前为止所做的。

#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]);
}

有什么想法如何找到它或者我有必要使用指针吗?

最佳答案

无需使用指针。由于您的代码中有许多错误/拼写错误,我发布了完整修改的1.代码。

#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]);
}
<小时/>

<子>1。一些有用的链接:
一、fscanf .
二. getchar .
三. strcpy .

关于c - 理解数组和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21415502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com