Trying to get transpose matrix, not sure what's wrong with my code!

Mar 4, 2014 at 8:09am
I'm trying to print the transposed matrix of the input matrix, but my output keeps coming out to be:

4 5 6
4 5 6
4 5 6

when I input:

1 2 3
4 5 6
7 8 9

I'm really not sure what it is that's wrong with my code. Any hints would be very helpful!

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
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstdlib>
#include <vector>

using std::vector;


int main(int argc, char** argv){

    vector<vector<int> > v(3, vector<int> (3,0));
   
    int a = atoi(argv[1]);
    int b = atoi(argv[2]);
    int c = atoi(argv[3]);
    int d = atoi(argv[4]);
    int e = atoi(argv[5]);
    int f = atoi(argv[6]);
    int g = atoi(argv[7]);
    int h = atoi(argv[8]);
    int k = atoi(argv[9]);

    v[0][0] = a;
    v[0][1] = b;
    v[0][2] = c;
    v[1][0] = d;
    v[1][1] = e;
    v[1][2] = f;
    v[2][0] = g;
    v[2][1] = h;
    v[2][2] = k;

    std::swap(d,b);
    std::swap(c,g);
    std::swap(f,h);

	for(int i = 0; i< v.size(); i++){
		vector<int> row = v.at(1);
		for(int j = 0; j < row.size(); j++){
			std::cout<<row.at(j)<<'\t';
		}
		std::cout<<'\n';
	}

}
Mar 4, 2014 at 8:16am
1
2
for(int i = 0; i< v.size(); i++){
	vector<int> row = v.at(1);
Are you sure that there should be 1?
Mar 4, 2014 at 8:30am
Whoops, silly mistake again haha. It should have been an 'i' instead of a 1!

Thanks!
Mar 4, 2014 at 8:37am
Perhaps a good reason not to use similar looking chars like i and j, or m and n

Make them words, so you can spot these errors straight away, or not make the error at all. Meaningful variable names count a lot towards understanding and self documentation as well.

Regards :+)
Topic archived. No new replies allowed.