Transpose Matrix

This algorithm finds the transpose of a given matrix. The transpose of a given matrix is formed by interchanging the rows and columns of a matrix.



									/*****Please include following header files*****/
// stdlib.h
/***********************************************/

typedef struct {
	int** Matrix;
	int Rows;
	int Columns;
} Matrix;

int** CreateMatrix(int rowCount, int colCount) {
	int** matrix = (int**)malloc(sizeof(int*) * rowCount);

	for (int i = 0; i < rowCount; ++i) {
		matrix[i] = (int*)malloc(sizeof(int) * colCount);
	}

	return matrix;
}

Matrix TransposeMatrix(Matrix matrix) {
	int** transpose = CreateMatrix(matrix.Columns, matrix.Rows);

	for (int i = 0; i < matrix.Rows; ++i)
		for (int j = 0; j < matrix.Columns; ++j)
			transpose[j][i] = matrix.Matrix[i][j];

	return{ transpose, matrix.Columns, matrix.Rows };
}
								


Example

									int** m1 = CreateMatrix(2, 3);
m1[0][0] = 1;
m1[0][1] = 2;
m1[0][2] = 3;
m1[1][0] = 4;
m1[1][1] = 5;
m1[1][2] = 6;

Matrix matrix = { m1, 2, 3 };
Matrix transpose = TransposeMatrix(matrix);
								


Output

									[0][0] = 1
[0][1] = 4
[1][0] = 2
[1][1] = 5
[2][0] = 3
[2][1] = 6