c - 理解数组和函数

标签 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<=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/

相关文章:

c - C如何 self 托管?

c - 如何使用C来限制SubString、Limit?

c - 关于动态分配内存给 char 指针的简单问题

PHP 数组项与数据库表部分匹配

javascript - 在异步存储中保存和删除每周计划 react native

c - 在多个函数中使用全局变量后发生段错误

perl 脚本的 c 包装器,该脚本接受在包装器中硬编码的参数

javascript - 如何在 Javascript 中动态命名数组?

c - 用于频繁随机访问的数组或链表?

php - 格式化关联数组声明