在C中通过引用传递二维数组 [英] Passing a two dimensional array by reference in C

查看:52
本文介绍了在C中通过引用传递二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助解决家庭作业问题.

I need help with a homework problem.

作业描述为:

用C语言设计一个程序,使用引用调用的方法,其中生成由 M 个学生组成的班级的成绩统计数据在一个学期中进行 N 次测验.测验全部权重相同,每个测验的成绩在 10 分以内点.该程序将按照指示的顺序执行以下操作程序执行时:

Design a program in C, using the method of call by reference, which generates statistics on the grades in a class consisting of M students and N quizzes given during the course of a semester. The quizzes all carry equal weighting, with the grade in each quiz being out of 10 points. The program is to do the following in the indicated sequence when the program is executed:

  1. 让用户在键盘上输入学生人数 M 和小测验数 N,其中 M 代表人数行数和 N 表示二维中的列数浮点数数组 x[M][N].
  2. 让用户在键盘上输入数组 x[M][N] 中所有元素的值.
  3. 在控制台显示输入的数组元素值的M行N列.
  4. 生成并在控制台上显示一维数组 p[M] 的元素,其中该数组的每个元素 p[M]代表每个 M 学期的平均成绩学生.
  5. 生成并在控制台上显示一维数组 q[N] 的元素,其中 q[N] 的每个元素代表N 个测验中的每个测验的平均成绩.
  6. 生成并在控制台上显示整个学期全班的平均成绩 z.

这是我迄今为止完成的代码:

This is the code that I've done so far:

#include <stdio.h>
#define students 300
#define quizzes 20

void averageGradeOfStudents(int M, int N, float p[], float *xPtr);
float averageGradeOfClass();
float averageGradeOfWholeClass();

int main() {
    int M, N;
    float x[students][quizzes];

    printf("Enter the number students (Maximum: 300): ");
    scanf_s("%d", &M);

    printf("\nEnter the number of Quizzes (Maximum: 20): ");
    scanf_s("%d", &N);

    for (int i = 0; i < M; i++) {
        for (int k = 0; k < N; k++) {
    printf("Enter the grade for student %d, Quiz %d: ", i, k);
            scanf_s("%f", &x[i][k]);
        }
    }
    printf("\n--------------------------------------------------------------------------------\n\n");

    for (int k = 1; k <= N; k++) {
        printf("\t\tQuiz%d", k);
    }

    for (int i = 0; i < M; i++) {
        printf("\nStudent%d Grades: ", i);
        for (int k = 0; k < N; k++) {
            printf("%.2f\t\t", x[i][k]);
        }

    }
    printf("\n\n\n--------------------------------------------------------------------------------\n\n");

    float p[students];
    averageGradeOfStudents(M, N, p, &x[0][0]);

    float q[quizzes];

    float z;

    getchar();
    return 0;
}

void averageGradeOfStudents(int M, int N, float p[], float *xPtr) {
    float grade[students];

    for (int i = 0; i < M; i++) {
        for (int k = 0; k < N; k++) {
            grade[i] += *(xPtr + i);
        }
    }

    for (int k = 0; k < M; k++) {
        p[k] = grade[k] / N;
        printf("The average grade of student%d is: %.2f\n\n", k, p[k]);
    }    
}
float averageGradeOfClass() {



}

float averageGradeOfWholeClass() {



}

我遇到的问题是我无法找到一种方法来分别对数组的每一行求和.

The problem I'm having is that I can't figure out a way to sum each row of the array individually.

推荐答案

您的作业进展非常顺利.获得完整的代码和努力令人耳目一新.在我们达到平均水平之前,有几件事会有所帮助.首先,也是良好的做法,初始化你的变量.对于数组尤其如此,因为它可以防止意外尝试读取未初始化的值,并且在字符数组的情况下可以为数组的第一个副本提供自动空终止(只要您只能复制大小为 1 的字符).

Very good progress on your assignment. It is refreshing to get full code and effort. There are a couple of things that will help before we get to an average. First, and always good practice, initialize your variables. This is especially true with arrays as it prevents an accidental attempt to read from an uninitialized value, and in the case of character arrays can provide automatic null-termination for the first copy to the array (as long as you only copy size-1 chars).

    int M = 0, N = 0;
    float x[students][quizzes] = {{ 0 }};
    float p[students] = {0};
    float q[quizzes] = {0};
    float z = 0;

传递数组始终是一个问题领域.传递声明为二维数组的数组时,必须将数组的宽度作为函数参数的一部分传递:

Passing arrays is always a problem area. When passing an array declared as a 2D array, you must pass the width of the array as part of the function argument:

void averageGradeOfStudents (int M, int N, float p[], float xPtr[][quizzes]);

然后你可以简单地通过传递数组本身来调用函数.

and then you can simply call the function by passing the array itself.

    averageGradeOfStudents (M, N, p, x);

(注意:当作为函数参数传递时,任何数组的第一级间接寻址都会转换为指针,但这是后面问题的主题.但是,供您参考,您的函数声明也可以正确地写成):

(note: the first level of indirection of any array is converted to a pointer when passed as a function argument, but that is a topic for a later question. However, for your reference, your function declaration could also be properly written as):

void averageGradeOfStudents (int M, int N, float p[], float (*xPtr)[quizzes]);

现在到平均水平,您已经接近了,您只需要通过更改索引 xPtr 的方式来对每个学生的元素求和(并初始化 grade 的值)代码>):

Now on to your average, you were close, you only needed to sum the elements per-student by changing the way you were indexing xPtr (and initialize the values of grade):

void averageGradeOfStudents (int M, int N, float p[], float xPtr[][quizzes]) 
{
    float grade[M];

    /* initialize variable length array to 0 */
    for (int i = 0; i < M; i++) grade[i] = 0;

    /* sum of grades for each student */
    for (int i = 0; i < M; i++) {
        for (int k = 0; k < N; k++) {
            grade[i] += xPtr[i][k];
        }
    }

    printf ("The average grades for the students are:\n\n");
    for (int k = 0; k < M; k++) {
        p[k] = grade[k] / N;
        printf("  student[%3d] is: %6.2f\n", k, p[k]);
    }    
}

在完整代码中使用它的一个简单示例可能是:

A quick example of its use in the full-code could be:

#include <stdio.h>
#define students 300
#define quizzes 20

void averageGradeOfStudents (int M, int N, float p[], float (*xPtr)[quizzes]);

int main (void) {

    /* good habit -- always initialize your variables */
    int M = 0, N = 0;
    float x[students][quizzes] = {{ 0 }};
    float p[students] = {0};

    printf("Enter the number students (Maximum: 300): ");
    scanf ("%d", &M);

    printf("\nEnter the number of Quizzes (Maximum: 20): ");
    scanf ("%d", &N);

    for (int i = 0; i < M; i++) {
        for (int k = 0; k < N; k++) {
            printf("Enter the grade for student %d, Quiz %d: ", i, k);
            scanf("%f", &x[i][k]);
        }
    }
    printf("\n--------------------------------------------------------------------------------\n\n");

    printf ("                        ");
    for (int k = 1; k <= N; k++) {
        printf(" Quiz%-2d", k);
    }
    putchar ('\n');

    for (int i = 0; i < M; i++) {
        printf("\n  Student[%3d] Grades: ", i);
        for (int k = 0; k < N; k++) {
            printf(" %6.2f", x[i][k]);
        }
    }
    printf("\n\n--------------------------------------------------------------------------------\n\n");

    averageGradeOfStudents (M, N, p, x);

    /* getchar(); */
    return 0;
}

void averageGradeOfStudents (int M, int N, float p[], float (*xPtr)[quizzes]) 
{
    float grade[M];

    /* initialize variable length array to 0 */
    for (int i = 0; i < M; i++) grade[i] = 0;

    /* sum of grades for each student */
    for (int i = 0; i < M; i++) {
        for (int k = 0; k < N; k++) {
            grade[i] += xPtr[i][k];
        }
    }

    printf ("The average grades for the students are:\n\n");
    for (int k = 0; k < M; k++) {
        p[k] = grade[k] / N;
        printf("  student[%3d] is: %6.2f\n", k, p[k]);
    }
    putchar ('\n');
}

使用/输出

$ ./bin/studentavg
Enter the number students (Maximum: 300): 3

Enter the number of Quizzes (Maximum: 20): 3
Enter the grade for student 0, Quiz 0: 8
Enter the grade for student 0, Quiz 1: 9
Enter the grade for student 0, Quiz 2: 8
Enter the grade for student 1, Quiz 0: 9
Enter the grade for student 1, Quiz 1: 9
Enter the grade for student 1, Quiz 2: 10
Enter the grade for student 2, Quiz 0: 7
Enter the grade for student 2, Quiz 1: 8
Enter the grade for student 2, Quiz 2: 9

--------------------------------------------------------------------------------

                         Quiz1  Quiz2  Quiz3

  Student[  0] Grades:    8.00   9.00   8.00
  Student[  1] Grades:    9.00   9.00  10.00
  Student[  2] Grades:    7.00   8.00   9.00

--------------------------------------------------------------------------------

The average grades for the students are:

  student[  0] is:   8.33
  student[  1] is:   9.33
  student[  2] is:   8.00

注意:我已将系统的 scanf_s 更改为 scanf,您需要将其改回.如果您还有其他问题,请告诉我.

Note: I've changed scanf_s to scanf for my system, you will need to change it back. Let me know if you have further questions.

这篇关于在C中通过引用传递二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆