为什么程序在从用户那里获取二维矩阵的值时会以无穷无尽的输入运行
Why the program runs with endless Input when taking values of a 2D matrix from user
我正在尝试编写一个程序来制作稀疏矩阵的链表实现和乘以两个稀疏矩阵。
假设用户选择尺寸 2*2 ,因此用户将有 4 个矩阵值输入。但是当我 运行 程序时,它需要用户无休止的输入。我不知道为什么。(这是错误)
而当我尝试在声明而不是接受输入时直接初始化矩阵的值时,代码有效,但我希望矩阵的值由用户输入。
我试图在 Code Block 和 VS Code 上 运行 这段代码。程序在两者上都一样。
是否存在逻辑错误?
int m=0,n=0;
int i,j;
int sparseMatric[10][10];
cout<<"Enter the Dimentions of the Matrix A"<<endl;
cin>>m>>n;
cout<<endl<<"Enter The Values of Sparse Matrix A"<<endl;
for(i=0; i<m; i++)
for(j=0; i<n; j++)
cin>>sparseMatric[i][j]; //whats wrong with input?
Sparse_Matrix* start = NULL;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if (sparseMatric[i][j] != 0)
create_new_node(&start, sparseMatric[i][j], i, j);
.
.
.
//code continues
for(j=0; i<n; j++)
应该是:
for(j=0; j<n; j++)
否则,循环将永远不会退出,因为 i
永远不会在内循环中被修改。
注意:我还强烈建议添加一个检查以确保 m
和 n
都小于 10,否则程序将崩溃。
我正在尝试编写一个程序来制作稀疏矩阵的链表实现和乘以两个稀疏矩阵。
假设用户选择尺寸 2*2 ,因此用户将有 4 个矩阵值输入。但是当我 运行 程序时,它需要用户无休止的输入。我不知道为什么。(这是错误)
而当我尝试在声明而不是接受输入时直接初始化矩阵的值时,代码有效,但我希望矩阵的值由用户输入。 我试图在 Code Block 和 VS Code 上 运行 这段代码。程序在两者上都一样。
是否存在逻辑错误?
int m=0,n=0;
int i,j;
int sparseMatric[10][10];
cout<<"Enter the Dimentions of the Matrix A"<<endl;
cin>>m>>n;
cout<<endl<<"Enter The Values of Sparse Matrix A"<<endl;
for(i=0; i<m; i++)
for(j=0; i<n; j++)
cin>>sparseMatric[i][j]; //whats wrong with input?
Sparse_Matrix* start = NULL;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if (sparseMatric[i][j] != 0)
create_new_node(&start, sparseMatric[i][j], i, j);
.
.
.
//code continues
for(j=0; i<n; j++)
应该是:
for(j=0; j<n; j++)
否则,循环将永远不会退出,因为 i
永远不会在内循环中被修改。
注意:我还强烈建议添加一个检查以确保 m
和 n
都小于 10,否则程序将崩溃。