锻炼需要我制作一个矩阵,用户可以在其中选择矩阵的大小。矩阵填充有随机数,然后显示在屏幕上。由于某种原因,无论矩阵的大小如何,第四个时隙每次均为0。
int main () {
int matriz[100][100] , filas , columnas ;
cout << "Ingrese el numero de filas: " ; cin >> filas ;
cout << "Ingrese el numero de columnas: " ; cin >> columnas ;
for (int i = 0 ; i < filas ; i++) {
for (int j = 0 ; j < columnas ; j++) {
//srand(time(NULL)) ;
matriz[i][j] = rand()%100 ;
}
}
for (int i = 0 ; i < filas ; i++) {
for (int j = 0 ; j < columnas ; j++) {
cout << matriz[i][j] << " " ;
}
cout << endl ;
}
return 0 ; }
除了for语句的第四次迭代外,它的行为方式应该是行为在3 * 3矩阵上的预期结果示例:
1 2 3
4 5 6
7 8 9
使用此代码后,我得到的是:
1 2 3
0 5 6
7 8 9
最佳答案
您的问题是您要声明int[][]
而不进行初始化。matriz[100][100]
是已声明但从未初始化的,通过调用int matriz[100][100]{}
您可以直接对其进行初始化。
关于c++ - 矩阵在某些位置没有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63379479/