Problem with matrix

Hello I am completely stuck with my matrix problem.
First funktion should read numbers one by one to the square matrix. Matrix rotate90deg function should create new matrix which has rotated 90 degrees compared to the original matrix. The last function should print original matrix. I have also header file where
typedef std::vector<std::vector<int>> Matrix;
is defined. What is wrong with this idea ?

Thanks for help :)
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

#include <iostream>
#include <vector>
#include "matrix.hpp"

// Assumes 'm' is a square matrix
Matrix readMatrix(int n) {
	
	Matrix matrix;
	for (int i = 0; i<n; i++) {
		for (int j = 0; j<n; j++) {
			std::cin >> matrix[j][i];
		
		}
		std::cout << "" << std::endl;

	}
	return matrix;
}

Matrix rotate90deg(Matrix & m) {
	Matrix temp = m;
	for (unsigned int i = 0; i<m.size(); i++) {
		for (unsigned int j = 0; j<m.size(); j++) {
			temp[i][j] = m[j][i];
		}
	}
	return temp;
}

void print(Matrix & m) {
	std::cout << "Printing out a " << m.size() << " x " << m.size() << " matrix:" << std::endl;
	for (unsigned int j = 0; j<m.size(); j++) {
		for (unsigned int i = 0; i<m.size(); i++) {
			if (i == m.size() - 1)
				std::cout << m[i][j] << " " << std::endl;
			else
				std::cout << m[i][j] << " ";
		}
	}
}
  
What is the size of 'matrix' after line 9?

The vector has both operator[] and member at(). They have a tiny difference. If you would use the at() on line 12, you would notice.


Why the printout on line 15?


On rotate90deg ... isn't that called transpose?
What if you don't have a square matrix?
Why the copy on line 22?


The print() again assumes a square matrix. The m is a vector and has m.size() vector<int> in it, but how many elements each of those m.size() vector<int> does have?
Topic archived. No new replies allowed.