问题引出的背景(二维数组的应用案例)
案例描述:由三名同学(张三,李四,王五),在一次考试中的成绩分别如下表,请分别输出三名同学的总成绩
姓名 | 语文 | 数学 | 英语 |
---|---|---|---|
张三 | 100 | 100 | 100 |
李四 | 90 | 50 | 100 |
王五 | 60 | 70 | 80 |
代码实现思路
设计一个函数,例如:void total_score_of_each_stu(param1, param2, param3,param4)
,其中param1
为二维数组维护的一个成绩单,第一维表示同学的个数,第二位为考试科目数;param2
为学生总数;param3
为考试科目的总数;param4
用来保存所有学生总成绩的一维数组。
引出的问题
param1
为二维数组,在函数void total_score_of_each_stu(param1, param2, param3,param4)
中该以怎样的形参形式去接收函数调用时实参传递的参数?
解决方法一:形参为二维数组
//@method 1: 形参为二维数组
/*
* @function: Output the total score of earch student
* @parma1[in]: The transcript
* @param2[in]: Number of students
* @param3[in]: Number of disciplines
* @param[out]: The total score of earch student
* @return: NA
* @author: Cohen
* @data: 2022/11/26
*/
void total_score_of_each_stu_01(int transc[][3], int num_stu, int num_discip, int* tal_score)
{
for (int i = 0; i < num_stu; i++)
{
int score = 0;
for (int j = 0; j < num_discip; j++)
{
tal_score[i] += transc[i][j];
}
}
}
void test01()
{
int transc[3][3] = {
{100,100,100},
{90,50,100},
{60,70,80}
};
string names[3] = { "张三","李四","王五" };
int num_stu = sizeof(transc) / sizeof(transc[0]);
int num_discip = sizeof(transc[0]) / sizeof(transc[0][0]);
int tal_score[3] = { 0 };
total_score_of_each_stu_01(transc, num_stu, num_discip, tal_score);
for (int i = 0; i < num_stu; i++)
{
cout << names[i] << ": " << tal_score[i] << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
解决方法二:形参为指向数组的指针
//@method 2: 形参为指向数组的指针
void total_score_of_each_stu_02(int (*transc)[3], int num_stu, int num_discip, int* tal_score)
{
for (int i = 0; i < num_stu; i++)
{
int score = 0;
for (int j = 0; j < num_discip; j++)
{
tal_score[i] += transc[i][j];
}
}
}
void test02()
{
int transc[3][3] = {
{100,100,100},
{90,50,100},
{60,70,80}
};
string names[3] = { "张三","李四","王五" };
int num_stu = sizeof(transc) / sizeof(transc[0]);
int num_discip = sizeof(transc[0]) / sizeof(transc[0][0]);
int tal_score[3] = { 0 };
total_score_of_each_stu_02(transc, num_stu, num_discip, tal_score);
for (int i = 0; i < num_stu; i++)
{
cout << names[i] << ": " << tal_score[i] << endl;
}
}
int main()
{
test02();
system("pause");
return 0;
}
解决方法三:形参为指向指针的指针,即二级指针
注意在调用此方法时,param1
必须为指针,而不能为数组名,否则会报错
//@method 3: 形参为指向指针的指针,即二级指针
void total_score_of_each_stu_03(int** transc, int num_stu, int num_discip, int* tal_score)
{
for (int i = 0; i < num_stu; i++)
{
int score = 0;
for (int j = 0; j < num_discip; j++)
{
tal_score[i] += transc[i][j];
}
}
}
void test03()
{
int transc[3][3] = {
{100,100,100},
{90,50,100},
{60,70,80}
};
int* p_transc[3];
p_transc[0] = transc[0];
p_transc[1] = transc[1];
p_transc[2] = transc[2];
string names[3] = { "张三","李四","王五" };
int num_stu = sizeof(transc) / sizeof(transc[0]);
int num_discip = sizeof(transc[0]) / sizeof(transc[0][0]);
int tal_score[3] = { 0 };
total_score_of_each_stu_03(p_transc, num_stu, num_discip, tal_score);
for (int i = 0; i < num_stu; i++)
{
cout << names[i] << ": " << tal_score[i] << endl;
}
}
int main()
{
test03();
system("pause");
return 0;
}
解决方法四:形参为一级指针
如果实参的二维数组的内存空间是malloc
或者new
出来的,那么可能会出现异常,因为malloc
或者new
出来的内存空间不一定是连续的,而这种方法transc[i * num_discip + j]
访问二维数组的元素,前提是二维数组内存空间物理连续
//@method 4: 形参为一级指针
void total_score_of_each_stu_04(int* transc, int num_stu, int num_discip, int* tal_score)
{
for (int i = 0; i < num_stu; i++)
{
int score = 0;
for (int j = 0; j < num_discip; j++)
{
tal_score[i] += transc[i * num_discip + j];
}
}
}
void test04()
{
int transc[3][3] = {
{100,100,100},
{90,50,100},
{60,70,80}
};
string names[3] = { "张三","李四","王五" };
int num_stu = sizeof(transc) / sizeof(transc[0]);
int num_discip = sizeof(transc[0]) / sizeof(transc[0][0]);
int tal_score[3] = { 0 };
total_score_of_each_stu_04((int*)transc, num_stu, num_discip, tal_score);
for (int i = 0; i < num_stu; i++)
{
cout << names[i] << ": " << tal_score[i] << endl;
}
}
int main()
{
test04();
system("pause");
return 0;
}
总结
- 方法一和方法二使用的前提是,需要提前知道传入的二维数组的第二维长度,代码的通用性不高,这里列出来只是为了具体说明所提出来的解决办法在代码中的具体应用,辅助理解。
- 方法二的通用最好,只是在构建二维数组时,我们需要构建一个指向二维数组的二级指针,而不能直接使用二维数组的名称作为实参传递给形参。
- 方法四通用性仅次于方法三,使用的前提是,二维数组的内存空间不是通过
malloc
或者new
在堆区开辟的内存空间,因为通过这两种方法开辟的内存空间不能够保证二维数组在物理内存上空间连续。
博客下期主题预告:malloc和new的区别,以及通过动态分配的内存,其物理内存空间是否一定连续?