C、多维数组练习 [英] C, Multi-Dimensional Arrays Exercise

查看:38
本文介绍了C、多维数组练习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对二维数组的理解是错误的.所以我会以另一种方式解决这个问题.假设我有以下变量.

My understanding of a 2d array is wrong. So I'll approach this question another way. Say I have the following variables.

int student_id[10], course_id[5];

int student_course[10][2];

最多只能有 10 个学生.只能有5门课程.一个学生只能选修 2 门课程.

There can only be 10 students. There can only be 5 courses. A student can only take 2 courses.

/*prompt user for student id*/

/*
say that the value for: 
student_id[0]=123
*/

/*prompt user for course id*/

/*
say that the value for:
course_id[0]=101
course_id[1]=102
course_id[2]=103
course_id[3]=104
course_id[4]=105
*/

我要打印的是
student_course[value_of_student_id][value_of_course_id].

所以我想这样做:

int student_id[10], course_id[5], student_course[10][2]

int i, j, k;

for(i=0; i<10;i++){
    for(j=0; j<5; j++){

        /*prompt user for student id*/
        printf("Enter User ID: ");
        scanf("%d", &student_id);

        /*prompt user for course id*/
        printf("Enter Course ID: ");
        scanf("%d", &course_id);

        for(k=0; k<2; k++){
            student_course[i][j]=student_course[i][k];
        }

    }
}

如果这是错误的方法,那么获得我想要的结果的正确方法是什么?也许是一个 3 维数组?我可能还没准备好.

If this is the wrong way, what is the correct way to get the results I want? A 3 dimensional array perhaps? I might not be ready for that yet.

推荐答案

你可以这样做

typedef struct
{
  int student_id;
  int course_id[2];
} Student

Student Students[10];

然后用 student_id 和他参加的课程初始化数组

then initialize the array with the student_id's and what courses he is attending

这篇关于C、多维数组练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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