Introduction

Transposing a matrix is an important operation in mathematics and computer science. It involves converting the rows of a matrix into columns and the columns into rows. For example, if we have a matrix AAA defined as:A=[123456]A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}A=[14​25​36​]

The transpose of matrix AAA, denoted as ATA^TAT, would be:AT=[142536]A^T = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}AT=​123​456​​

This operation is commonly used in various fields, including linear algebra, computer graphics, and data processing.

#include <stdio.h>

#define MAX 10  // Maximum size of the matrix

void inputMatrix(int matrix[MAX][MAX], int rows, int cols) {
    printf("Enter elements of the matrix (%d x %d):\n", rows, cols);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

void transposeMatrix(int matrix[MAX][MAX], int transpose[MAX][MAX], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            transpose[j][i] = matrix[i][j];  // Swap rows and columns
        }
    }
}

void printMatrix(int matrix[MAX][MAX], int rows, int cols) {
    printf("Matrix (%d x %d):\n", rows, cols);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int matrix[MAX][MAX], transpose[MAX][MAX];
    int rows, cols;

    printf("Enter number of rows and columns: ");
    scanf("%d %d", &rows, &cols);

    inputMatrix(matrix, rows, cols);
    transposeMatrix(matrix, transpose, rows, cols);
    
    printf("Original Matrix:\n");
    printMatrix(matrix, rows, cols);
    
    printf("Transposed Matrix:\n");
    printMatrix(transpose, cols, rows);

    return 0;
}

Explanation

  1. Includes and Defines: The program begins by including the stdio.h header for input/output functions and defining a constant MAX for the maximum size of the matrix.
  2. Input Function: inputMatrix prompts the user to enter elements for the matrix. It uses nested loops to read values into a two-dimensional array.
  3. Transpose Function: transposeMatrix swaps elements of the original matrix to create the transpose. For each element at position (i, j) in the original matrix, it assigns its value to (j, i) in the transposed matrix.
  4. Print Function: printMatrix displays the matrix in a readable format, iterating through each element.
  5. Main Function: In main, the program:
    • Declares two matrices: the original and its transpose.
    • Asks for the number of rows and columns.
    • Calls the input, transpose, and print functions to display the results.

Input

The user is prompted to enter the number of rows and columns, followed by the matrix elements. Example input:

Enter number of rows and columns: 3 2
Enter elements of the matrix (3 x 2):
1 2
3 4
5 6

Output

The program will output both the original and transposed matrices. Continuing from the previous example, the output will be:

Original Matrix:
1 2 
3 4 
5 6 

Transposed Matrix:
1 3 5 
2 4 6 

Conclusion

This C program effectively demonstrates how to compute the transpose of a matrix using basic array manipulation. Understanding how to transpose matrices is crucial for various applications in mathematics and computer science. The code can be easily modified to handle larger matrices or different data types, making it a versatile example for learning matrix operations in C.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights