【问题标题】:Using structures in C with user defined functions将 C 中的结构与用户定义的函数一起使用
【发布时间】:2020-12-31 13:15:14
【问题描述】:

我遇到了一个问题,我用不同的方法尝试了很多次,但仍然无法获得所需的解决方案,请帮助我。 问题:定义一个结构来存储学生的详细信息。将它们传递给一个函数,其中 CGPA 最高的学生是从一组 5 名学生中计算出来的。并显示结果。(这里我们必须存储每个学生的姓名、年龄和 CGPA) 这是我对这个问题的尝试:

#include <stdio.h>
void highCGPA(float b,struct detst D[4]);

struct detst
{
    int age;
    float CGPA;
    char name[30];
};

int main()
{
    struct detst D[4];
    int i;
    float h;
    for(i=0;i<=4;i++)
    {
    printf("Enter the name of student %d:\n",i+1);
    scanf("%s",&D[i].name);
    printf("Enter the age of student %d:\n",i+1);
    scanf("%d",&D[i].age);
    printf("Enter the CGPA obtined by student %d:\n",i+1);
    scanf("%f",&D[i].CGPA);
    }
    highCGPA(h,D);
}

void highCGPA(float b,struct detst D[4])
{
    int i,max;
    
    max = D[0].CGPA;
    
    for(i=0;i<=4;i++)
    {
        if(D[i].CGPA > max)
        {
            max = D[i].CGPA;
        }
    }
    printf("Highest CGPA obtained is:\n%f",max); 
}

【问题讨论】:

  • i &lt;= 4 ??可能应该考虑制作这个i &lt; 4
  • 你有什么问题?

标签: c structure user-defined-functions


【解决方案1】:

问题不止一个。您正在尝试将 5 个学生的数据存储在一个大小为 4 的结构数组中。所以:

struct detst D[5]; // fixed size
int i;
float h;
for(i=0;i<5;i++) { // EDIT (comments suggestion): for loop fix following standard convetions
    /* take input */
}

最后,如果您将max 声明为int,则不能尝试使用float 的格式说明符来打印它。所以:

printf("Highest CGPA obtained is: %d\n",max); // format specifier for int is %d (not %f)

【讨论】:

  • 你确定他要5个学生吗?您的 for-loop 仍然不遵循标准约定。 max 可能应该定义为 float 而不是更改格式说明符。 (max作为CGPA的最大值,即float
  • 非常感谢先生。我已经对我的代码进行了必要的更改,现在它工作正常。谢谢你救了我。
  • @HAL9000 他从字面上说该函数需要“一组 5 名学生”。考虑到有关所需输出格式的信息很少,我不知道将 max 更改为 float 或将 CGPA 更改为 int 是否更好。我离开了 for 循环,就像在示例中一样,因为它不是解决他的问题的严格要求。
  • 抱歉,忽略了他想要 5 个学生。 max 变量的问题,更重要的是,for 循环仍然存在。在您的答案中复制错误代码时,您至少应该对它为什么不好给出一个小评论。在这种情况下,代码会让读者感到困惑,什么应该是适当的修复。
  • 我同意for 部分。但我仍然认为,由于我们不知道他想要输出的内容,我们无法判断应该将max 更改为float 还是将CGPA 更改为int。可能我至少应该指定应该进行一个或另一个修复。
【解决方案2】:

还有其他几个问题。

  1. 您的功能声明highCGPA 高于您的struct detst 定义 你应该有错误

错误:数组类型的元素类型“struct detst”不完整

在结构定义之后声明你的函数。

  1. 您的 max 变量在函数 highCGPA 中是 int,但您正试图在其中存储 float 值。

【讨论】:

  • 我使用 DEV C++ 作为我的编辑器和编译器,所以我在 struct detst 上方声明 highCGPA 时没有遇到任何错误我认为如果你以这种方式声明,你就是在全局声明它,即, USER DEFINED FUNCTION 可以在代码的任何地方使用。
【解决方案3】:

对程序功能稍作改动即可改进:

#include <float.h> // FLT_MAX
#include <stdio.h>

struct detst
{
   int age;
   float CGPA;
   char name[30];
};

// Return offset of student with highest CGPA
int highCGPA(struct detst const (*D)[5]);  // Using pointer to array 
                                           // to avoid degeneration

int main()
{
   struct detst D[5];
   int i;
   for(i = 0; i < 5; i++)
   {
      printf("Enter the name of student %d:\n", i + 1);
      scanf("%29s", D[i].name);  // Make sure we don't overwrite the buffer
      printf("Enter the age of student %d:\n", i + 1);
      scanf("%d", &D[i].age);
      printf("Enter the CGPA obtained by student %d:\n", i + 1);
      scanf("%f", &D[i].CGPA);
   }
   int bestStudentOffset = highCGPA(&D); // By asking for best student
                                         // we can obtain a lot more
   printf(
      "Best student is %d year old %s with\n",
      D[bestStudentOffset].age,
      D[bestStudentOffset].name);
   printf("Highest CGPA obtained is:\n%f\n", D[bestStudentOffset].CGPA);
}

// Return offset of student with highest CGPA
int highCGPA(struct detst const (*D)[5])
{
   int i, maxOffset = -1;
   float max = -FLT_MAX;

   for(i = 0; i < 5; i++)
   {
      if((*D)[i].CGPA > max)
      {
         max       = (*D)[i].CGPA;
         maxOffset = i;
      }
   }
   return maxOffset;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 2021-09-04
    • 2017-01-04
    • 1970-01-01
    • 2018-09-29
    相关资源
    最近更新 更多