【问题标题】:How can I take the average of only certain rows and columns of a 2D array?如何仅取二维数组的某些行和列的平均值?
【发布时间】:2018-11-11 02:47:59
【问题描述】:

我必须只取测试成绩的平均值并将它们放入最后一列,但我不知道该怎么做。我有一个二维数组的函数,它接受一个包含成绩的文件名,但第一列是学生 ID,所以我不需要取平均值。

这是二维数组函数的代码。

#define x 10
#define y 6

void getData(float arr1[x][y])
{
  FILE* graFile;
  float arr2[x][y];
  char userIn[50];
  printf("Enter the text filename: ");
  scanf("%s", userIn);
  graFile = fopen(userIn, "r");
  int studentId, test1, test2, test3, test4;
  for(int i = 0; i < x; i++)
  {
    if(graFile != NULL)
    {
      fscanf(graFile, "%d%d%d%d%d", &studentId, &test1, &test2, &test3, &test4);
      arr2[i][0] = studentId;
      arr2[i][1] = test1;
      arr2[i][2] = test2;
      arr2[i][3] = test3;
      arr2[i][4] = test4;
    }
    else
    {
      printf("\nThis file does not exist.");
      return;
    }
  }
  printf("\n %11s%11s%11s%11s%11s%11s", "Student Id","Test 1","Test 2","Test 3","Test 4","Final\n");
  printf("*********************************************************************\n");
  for(int i = 0; i < x; i++)
  {
    for(int j = 0; j < y; j++)
    {
      printf("%11.0f", arr2[i][j]);
    }
    printf("\n");
  }
  printf("*********************************************************************\n");
  fclose(graFile);
  return;
}

这给了我这个输出

Enter the text filename:  grades.txt

  Student Id     Test 1     Test 2     Test 3     Test 4     Final
*********************************************************************
       6814         85         86         92         88          0
       7234         76         81         84         78          0
       6465         87         54         68         72          0
       7899         92         90         88         86          0
       9901         45         78         79         80          0
       8234         77         87         84         98          0
       7934         76         91         84         65          0
       7284         56         81         87         98          0
       7654         76         87         84         88          0
       3534         86         81         84         73          0
*********************************************************************

现在我只需要创建一个新函数来平均该数组中的测试成绩并将其放入最后一列,但我无法做到这一点。我很感激能得到任何帮助。

【问题讨论】:

  • arr2[i][5] = (test1 + test2 + test 3 + test4) / 4;
  • 除了上面显而易见的答案之外,您的程序还有几个问题: 1. if(graFile != NULL) 需要在 for 循环之外,而不是在里面。 2.需要检查fscanf的返回值;除非您绝对确定该文件始终包含至少 x 行并且格式始终与您的 fscanf 一致。
  • 你知道我如何通过它自己的功能做到这一点吗?该文件仅包含该数量的行,因为这是一个学校项目,因此永远不会被修改。因此,一旦我将 if 条件放在循环之外,我的代码就可以了吗?
  • 一个函数以float[x][y]为参数,其中第一列是学生id,2-5列是分数,函数将平均值保存到最后(第6)列。这是你需要的吗?
  • 是的,就是这样。

标签: c arrays function average


【解决方案1】:
void setAverage(float arr[][y], int x) {
    for(int i = 0; i < x; i++) {
        arr[i][5] = (arr[i][1] + arr[i][2] + arr[i][3] + arr[i][4]) / 4;
    }
}

【讨论】:

  • 非常感谢!快速的问题,我应该在哪里添加我的 printf 语句?
  • 虽然这可能会回答作者的问题,但它缺少一些解释性文字和/或文档链接。如果没有围绕它们的一些短语,原始代码 sn-ps 并不是很有帮助。您可能还会发现how to write a good answer 非常有帮助。请编辑您的答案。
猜你喜欢
  • 1970-01-01
  • 2023-02-09
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多