Reading a matrix into 2d array

Hi all. First off, this is for homework and I want to learn it on my own, but I could use some pointers/direction.

I need to read a series of matrices from a text file into a 2d array. Matrices like this:

3
1 2 3
4 5 6
7 8 9

where the top 3 indicates the size of it.

Here's what I got:

1
2
3
4
5
6
7
8
9
10
11
  	while (inputFile >> n)
	{	
				for( int n=0; n<5; n++)
				{
					inputFile >> square[ROW][COL];
					ROW++;
					COL++;
					cout << "\n" << square[ROW][COL];
	
				}		
	}
@dgadk

To learn how to create an array of user defined dimensions,
read here..
http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new


As for this..
1
2
3
4
5
6
7
for( int n=0; n<5; n++)
	{
		inputFile >> square[ROW][COL];
		ROW++;
		COL++;
		cout << "\n" << square[ROW][COL];
	}	

You're not going to see what was inputted, as you are showing the next array's location by putting the ROW++ and COL++, before the cout.
Last edited on
Here's what I have now:

Not getting it to:

a) read only one matrice
b) read the right values into the 2darray

Any and all tips, thoughts, etc are appreciated.

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

inputFile >> m;
	cout << "m= " << m;


		for (i=0; i < (m*m); i++)
		
		{
			 for (ROW=0; ROW < m; ROW++)
			{
			
				
				for (COL=0; COL < m; COL++)
				{

					inputFile >> square[ROW][COL];
					cout << "\narray: " << square[ROW][COL];
					
				}
			}
		
		}

Last edited on
Topic archived. No new replies allowed.