我现在正在使用2个2D阵列,我需要制作通用矩阵程序。现在代码是输入。如果我在/ *和* /之间添加一个循环,剩下的循环工作正常!
#include <stdio.h>
int main()
{
int i, j, k, l; //I, J for array matrix a, K, L for matrix b
int a[i][j]; //matrix of size i by j
int b[k][l];
int rowa=0, cola=0, rowb=0, colb=0;
//rowa is ROW no. of array a and colb e.g Column no. for array b
printf("size of matrix a: ");
scanf("%d\n%d", &i, &j); //i is row, j is column
printf("size of matrix b: ");
scanf("%d\n%d", &k, &l); //k is row, l is column
while (rowa < i)
{
while (cola < j)
{
statements
}
statements
}
while (rowb < k)
{
while (colb < l)
{
statements
}
statements
}
return 0;
}
那我该怎么办?感谢
答案 0 :(得分:5)
您使用未初始化的变量会导致 未定义的行为 :
int i, j, k, l; //I, J for array matrix a, K, L for matrix b
int a[i][j]; //matrix of size i by j
...
printf("size of matrix a: ");
scanf("%d\n%d", &i, &j); //i is row, j is column
您应该在i
声明之前初始化j
和a[i][j]
,即
printf("size of matrix a: ");
scanf("%d\n%d", &i, &j);
// initialize the matrix:
int a[i][j];