Transpose Of A Matrix

Hi, I Am Making Program That Take Input From User For Matrix And Transpose Them
But Its not Showing Output Of Transpose Please Check The Code And Describe It In A beginner Level I Don`t Know Much About C\C++

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
#include<iostream>
using namespace std;

main()
{
const int MaxRows =3, MaxCols = 3;
int Rows, Cols;
int Temp;
int Array[MaxRows][MaxCols];
		//Take Input From User For Matrix
	for(Rows = 0; Rows< MaxRows; Rows++)
	{
		for(Cols = 0; Cols< MaxCols; Cols++)
		{
			cout<<"please Enter VAlue Of ["<<Rows<<","<<Cols<<"] ";
			cin>>Array[Rows][Cols];	
		}	
		cout<<endl;
	}
	// Transpose Of A Matrix
	for(Rows =0; Rows< MaxRows; Rows++)
	{
		int Colsp = 0;
		for(Colsp = Colsp; Cols<MaxCols; Colsp++)
		{
			Temp = Array[Rows][Colsp];
			Array[Rows][Colsp] = Array [Colsp][Rows];
			Array[Colsp][Rows] = Temp;
			cout<<"\t"<<Array[Rows][Cols];
		}
		Colsp += 1;
		cout<<endl;
	}
}



Hello Najam489,

Your inner for loop is a problem. As your code is it will not execute. I have tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
	for (Rows = 0; Rows< MaxRows; Rows++)
	{
		//int Colsp = 0;
		for (int Colsp = 0; Colsp < MaxCols; Colsp++)
		{
			Temp = Array[Rows][Colsp];
			Array[Rows][Colsp] = Array[Colsp][Rows];
			Array[Colsp][Rows] = Temp;
			std::cout << "\t" << Array[Rows][Cols];
		}
		//Colsp += 1;
		std::cout << std::endl;
	}


But he output does not work.

Given the input numbers 1 - 9 what would be your expected output be?

Hope that helps,

Andy
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
If you work with a 1d structure instead of 2, and copy into a new destination, it is really simple. 

for(k = 0; k < rows; k++)
      {
         for(j = 0; j < cols; j++)
            result[j*rows + k] = orig[k*cols +j];
      }

or the same thing in 2-d

for(k = 0; k < rows; k++)
      {
         for(j = 0; j < cols; j++)
            result[j][k] = orig[k][j];
      }


don't forget that result is [cols][rows] in terms of dimensions if not the same. 
 
Last edited on
Topic archived. No new replies allowed.