Transposed two-dimensional matrix

Hello,
I want transposed two-dimensional matrix.
What i do? :(

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include <iostream>
using namespace std;


	void Transposed (int matrix [100][100],int length)
	{
		int swap = 0;
		for (int i = 0; i < length; i++)
		{
			for (int j = 0; j < length; j++)
			{
					swap = matrix[i][j];
					matrix [i][j] = matrix[j][i];
					matrix[j][i] = swap;
		}
		
	}
	
	}
		void main ()
		{
	int matrix [100][100]={0};
	int length;
	cout<<"Enter Number\n";
	cin>>length;

	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < length; j++)
		{
			cin>>matrix[i][j];
			
		}
		cout<<endl;
	}

	cout<<"=====================Default Matrix=====================\n ";

		for (int i = 0; i < length; i++)
	{
		
		for (int j = 0; j < length; j++)
		{
			cout<<matrix[i][j]<<" ";
			
		}
		cout<<endl;
	}

		Transposed(matrix,length);
		
		cout<<"=====================After Transposed=====================\n";

		for (int i = 0; i < length; i++)
	{
		
		for (int j = 0; j < length; j++)
		{
			cout<<matrix[i][j]<<" ";
			
		}
		cout<<endl;
	}
		
}
		
few things to consider :

is it a square matrix all the time ? if so , there is a way to remove a for loop

is it a matrix with less than or equal 4 rows and cols ? if so , directly use indices . Easy on Google.

if the matrix is square you dont need to swap i and j when i == j.
Topic archived. No new replies allowed.