Hi
When I input 3 to m and 2 to n it gives me this output:
1 2 3
3
2
5
I think it should be :
1 2 3
3
2
3
Why doesn't it show the second output?
How can i fix it for it to output the second output?
I tried this program on c++11online and there too gives the first output.
#include <iostream>
usingnamespace std;
int main ()
{
int m,n;
cin>>m>>n;
int square[m][m];
for (int i =0;i<=m;i++)
{
square[0][i]=n+i;
square[i][0]=n+i;
}
cout<<square[1][0]<<'\n';
}
1) you have 2 main functions, you should only have 1
2) you cant initialise an array like you have on line 11, it needs to be dynamically allocated using new: http://www.cplusplus.com/doc/tutorial/dynamic/
Actually the problem was here : for (int i =0;i<=m;i++)
instead of i<=m it should be i<m
cause there is not an array square[m][m], its just until square[m-1][m-1].
square is an 2D array of ints, and to create a array at compile time you need to supply a constant size, unless you are using some compiler which doesn't care then it just won't work, which is why the new operator exists in the first place to do it dynamically.
Lastly, it was impossible that your original post code would run because of the duplicate main() amongst other things - I notice you have modified that original post though.