C,多维数组练习 [英] C, Multi-Dimensional Arrays Exercise
问题描述
我的一个二维数组的理解是错误的。所以,我会处理这个问题的另一种方式。说我有以下变量。
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]
。
What I want to print out is the
student_course[value_of_student_id][value_of_course_id]
.
因此,我认为做这样的:
So I thought of doing it this way:
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];
}
}
}
如果这是错误的方式,什么是正确的方式来获得我想要的结果吗? A 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.
推荐答案
您可以不喜欢这样,而不是
You could do it like this instead
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屋!