Hello, I am trying to get this very simple program to work and I can't figure it out. The program is supposed to have the user input values of a matrix and then simply print the matrix. Here's the code:
#include <iostream>
usingnamespace std;
int main () {
int a,b; // matrix dimensions
int n_input,m_input;
cout << "Please enter the number of rows in the matrix: ";
cin >> a;
a--;
cout << "Please enter the number of columns in the matrix: ";
cin >> b;
b--;
int array [a][b];
for (m_input=0; m_input<=b;m_input++) {
for (n_input=0; n_input<=a; n_input++) {
cout << "Please input "<< (n_input+1) << "x" << (m_input+1)<< ":";
cin >> array[n_input][m_input];
}
}
// print the matrix
int n, m;
for (m=0; m<=b; m++) {
for (n=0; n<=a; n++) {
cout << array [n][m] << " ";
}
cout<<endl;
}
return 0;
}
The print array section works if I put in the values of the array when I declare it, so that leads me to believe that I'm making a mistake in the input of the values section. Any help is greatly appreciated!
Why do you subtract one from the sizes of the array?
Also, you cannot declare an array like you are on line 14 - it is not valid C++ code. If your compiler supports that then it is probably an extension and there is no telling how it works or how reliable it is.