Importing 2D array

Essentially what I need to do is take a text file, ("input.txt"):
4 4
1 0 0 1
1 1 1 1
0 0 1 0
0 0 1 0

And take the first two values on line 1 (4, 4) and use them as length and width.
Number of rows: 4
Number of columns: 4

Then I need to print out the matrix and further manipulate it.
I need to find the sum of the 1's per column and then take that number and replace each 1 with the 1's in each column.
So it'll look like this:
2 0 0 2
2 1 3 2
0 0 3 0
0 0 3 0

The part that's mostly troubling me is that my instructor will be giving me the input file with random values, so I don't know what the matrix dimensions will be.
I can read the 2D array but can't seem to use it after.
I need to find a way to skip the first line, and then read in the matrix and be able to use it mathematically to add up each column.
Any tips or advice would be greatly appreciated.
Since the size of matrix isn't fixed you can't use static arrays. Use dynamically allocated arrays http://www.cplusplus.com/doc/tutorial/dynamic/
or std::vector http://www.cplusplus.com/reference/vector/vector/
or boost matrix library http://www.boost.org/doc/libs/1_42_0/libs/numeric/ublas/doc/matrix.htm instead.
Last edited on
Topic archived. No new replies allowed.