1、题目
请编写函数fun,函数的功能是:将M行N列的二维数组中的数据按列的顺序依次放到一维数组中。
例如,二维数组中的数据为:
33 33 33 33
44 44 44 44
55 55 55 55
则一维数组中的内容应是:
33 44 55 33 44 55 33 44 55 33 44 55
2、程序分析
解题思路:本题提供的参考程序利用内、外循环两层for循环,按列的顺序将第1到3行的数据依次存放到数组中的相应位置上,同时利用形参变量n来统计一维数组中存放的数据的个数,这就实现了将M行N列的二维数组中的数据按行的顺序依次放到一维数组中。
3、程序源代码
#include <stdio.h>
void fun(int s[][10], int b[], int *p, int m, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
*b++ = s[j][i];
}
}
}
int main(int argc, char const *argv[])
{
//int a[3][10];
int b[30];
int a[3][10]={
{33,33,33},{44,44,44},{55,55,55}};
fun(a, b, b, 3, 4);
for (int i = 0; i < 3 * 3; i++)
{
printf("%d",b[i]);
}
}
4、输入输出
标准输出:
33 44 55 33 44 55 33 44 55 33 44 55