Noob's question - please heeelp asap!

Hi.. I have the following code which should read a matrix and then type its content.. But when I run it, it only types the last column!

Whats going on? Please answer as soon as possible, its important! Thank you!

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

using namespace std;

int main()
{
	int m,n,a[m][n],i,j;
	cout<<"type the numbers for rows and columns"<<endl;
	cin>>m;
	cin>>n;
	
	cout<<"type the content of the matrix"<<endl;
	for (i=0; i<m; i++)
		for (j=0; j<n; j++)
			cin>>a[i][j];
			
	for (i=0; i<m; i++)
		for (j=0; j<n; j++)
			cout<<a[i][j];
		
		
	return 0;		
}
Last edited on
first problem, you are defining a as an array, I'm surprised your compiler isn't yelling at you- if you want to create it dynamically you have to make an int pointer and allocate it with new. In your case m and n aren't even initialized so it wouldn't work even if that was the correct method.

you can use a vector(or deque or other stl container) or dynamic arrays to accomplish your goals. I would recommend a vector.
Last edited on
Hmm.. I dont understand.. Would it be easy for you to write the code so that I can see what you mean?
Topic archived. No new replies allowed.