如何使用C语言在动态数组中手动输入一个二维数组的值

也就是说相当于程序最后的运行结果应该是这样的:
输入一维数组的大小:3
输入二维数组的大小:3
输入第一行的值:a b c
输入第二行的值:d e f
输入第三行的值:g h k
得到的数组如下:
a b c
d e f
g h k

以上的这个过程用C语言如何实现

c++强答一波
#include <iostream>
using namespace std;
int main()
{
cout << "请输入你需要的矩阵a的行数和列数" << endl;
int s, t;//s为数组行数的递推
int i, j;//i为二维数组的行数,j为二位数组的列数
cin >> i >> j;
int a[100][100];
int b[100][100];
cout << "请输入你需要的矩阵b的行数和列数" << endl;
int m, n;
cin >> m >> n;//m为二维数组的行数,n为二位数组的列数
if (i != m || j != n)//矩阵a与b的哈那个书和列数需相等,否则无法比较
{
cout << "error" << endl;
}
cout << "请输入你需要的矩阵a的数值" << endl;
for (s = 0; s < i; s++)
{
for (t = 0; t < j; t++)
{
cin >> a[s][t];//输入矩阵a
}
}
cout << "请输入你需要的矩阵b的数值" << endl;
for (s = 0; s < i; s++)
{
for (t = 0; t < j; t++)
{
cin >> b[s][t];//输入矩阵b
}
}
cout << "a的矩阵为" << endl;
for (s = 0; s < i; s++)
{
for (t = 0; t < j; t++)
{
cout << a[s][t] << " ";//输出矩阵a
}cout << endl;
}
cout << "b的矩阵为" << endl;
for (s = 0; s < i; s++)
{
for (t = 0; t < j; t++)
{
cout << b[s][t] << " ";//输出矩阵b

}cout << endl;
}
system("pause");
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-04-03
int row, col;
printf("input col num\n");
scanf("%d",&col);
printf("input row num\n");
scanf("%d",&row);

char **matrix = (char**)calloc(row,sizeof(char*))
for(int m=0; m<row;m++) matrix[m] = (char*)calloc(n, sizeof(char));

printf("input 1st line"); scanf("%c %c &c", &matrix[0][0], &matrix[0][1],&matrix[0][2]) ;
printf("input 2nd line"); scanf("%c %c &c", &matrix[1][0], &matrix[1][1],&matrix[1][2]) ;
printf("input 3rd line"); scanf("%c %c &c", &matrix[2][0], &matrix[2][1],&matrix[2][2]) ;

这个方法是你希望的,试验一下
第2个回答  2015-04-03
C语言是不支持动态数组的,你可以创建一个比较大的数组,或者用链表来存储。本回答被提问者采纳
相似回答