Output problem...


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 #include <iostream>

using namespace 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';

	}

Thanks in advance.

Last edited on


Some problems with your code.

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/

Last edited on
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.
Topic archived. No new replies allowed.