Read 2D arrays from file and store them in vectors
Aug 19, 2012 at 10:55am UTC
I have this file which contains 8x8 blocks of numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
1 2 3 1 30 40 27 8
2 4 19 8 10 17 14 16
3 2 9 7 15 18 21 24
4 8 0 1 20 24 28 32
5 10 15 20 25 30 35 40
0 12 18 24 30 36 42 48
3 14 21 28 35 42 49 56
8 16 24 32 40 48 56 64
9 10 11 12 13 14 15 16
18 20 22 24 26 28 30 32
27 30 0 1 39 42 45 48
36 40 1 48 52 56 60 64
45 1 55 60 65 70 75 80
54 60 66 72 78 84 90 96
63 70 77 84 91 98 105 112
72 80 88 96 104 112 120 128
9 18 27 36 45 1 1 72
10 20 30 40 50 60 70 80
11 22 33 44 55 66 77 88
12 24 36 1 60 72 84 96
13 26 39 52 65 78 91 104
14 28 42 56 70 84 98 112
0 30 45 60 75 90 105 120
16 32 48 64 80 96 112 128
81 90 99 108 0 126 1 144
90 100 110 120 130 140 150 160
99 110 121 132 143 154 165 176
108 120 132 144 156 168 180 192
117 130 143 156 169 182 195 208
126 140 154 168 182 196 210 224
135 150 165 180 195 210 225 240
144 160 176 192 208 224 240 256
How can I read and store the blocks into a vector of vectors. I want all of the blocks to be stored in a vector of vectors like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 1 30 40 27 8 9 10 11 12 13 14 15 16
2 4 19 8 10 17 14 16 18 20 22 24 26 28 30 32
3 2 9 7 15 18 21 24 27 30 0 1 39 42 45 48
4 8 0 1 20 24 28 32 36 40 1 48 52 56 60 64
5 10 15 20 25 30 35 40 45 1 55 60 65 70 75 80
0 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
3 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112
8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128
9 18 27 36 45 1 1 72 81 90 99 108 0 126 1 144
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160
11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 176
12 24 36 1 60 72 84 96 108 120 132 144 156 168 180 192
13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 208
14 28 42 56 70 84 98 112 126 140 154 168 182 196 210 224
0 30 45 60 75 90 105 120 135 150 165 180 195 210 225 240
16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256
So that if I execute the code below
1 2 3 4
for (int i = 0; i < 8; ++i)
for (int j = 0; j < 8; ++j)
cout << matrix[i][j] << " " ;
cout << "\n" ;
It will give me the first block from the file which is
1 2 3 4 5 6 7 8
1 2 3 1 30 40 27 8
2 4 19 8 10 17 14 16
3 2 9 7 15 18 21 24
4 8 0 1 20 24 28 32
5 10 15 20 25 30 35 40
0 12 18 24 30 36 42 48
3 14 21 28 35 42 49 56
8 16 24 32 40 48 56 64
Here is what I have so far but the blocks are not arranged in the sequence that I want.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
readFile.open( "number.txt" , fstream::in );
// size of array above 16x16
int n = 16;
// I use an array but I want to use 2D vectors instead of arrays
// if I use vector vector< vector<int> > matrix it gives me segmentation
// fault
int matrix[n][n];
while ( !readFile.eof() )
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
readFile >> matrix[i][j];
cout << newMatrix[i][j] << " " ;
}
cout << "\n" ;
}
cout << "\n" ;
}
Sample Output:
1 2 3 4 5 6 7 8 9
1 2 3 1 30 40 27 8 2 4 19 8 10 17 14 16
3 2 9 7 15 18 21 24 4 8 0 1 20 24 28 32
5 10 15 20 25 30 35 40 0 12 18 24 30 36 42 48
3 14 21 28 35 42 49 56 8 16 24 32 40 48 56 64
9 10 11 12 13 14 15 16 18 20 22 24 26 28 30 32
27 30 0 1 39 42 45 48 36 40 1 48 52 56 60 64
45 1 55 60 65 70 75 80 54 60 66 72 78 84 90 96
63 70 77 84 91 98 105 112 72 80 88 96 104 112 120 128
...
Any tips??
Aug 19, 2012 at 12:46pm UTC
So what is the algorithim for transposing from the first matrix to the second?
Once you understand the rule, you should be to write the code.
Topic archived. No new replies allowed.