C Program to Find the Transpose of a Matrix

Introduction

The transpose of a matrix is obtained by swapping the rows with the columns. In other words, the element at position (i, j) in the original matrix is moved to position (j, i) in the transposed matrix. This guide will show you how to write a C program that computes the transpose of a given matrix.

Problem Statement

Create a C program that:

  • Takes a matrix as input from the user.
  • Computes the transpose of the matrix.
  • Displays the transposed matrix.

Example:

  • Input:
    • Matrix:
      1 2 3
      4 5 6
      
  • Output: Transposed Matrix:
    1 4
    2 5
    3 6
    

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the input matrix, the transposed matrix, and loop counters.
  4. Input the Dimensions and Elements of the Matrix: Use loops to take input from the user for the matrix.
  5. Compute the Transpose of the Matrix: Use nested loops to swap the rows and columns of the matrix.
  6. Display the Transposed Matrix: Use loops to display the transposed matrix.

C Program to Find the Transpose of a Matrix

#include <stdio.h>

int main() {
    // Step 1: Declare variables to hold the matrix and dimensions
    int rows, cols;
    int matrix[100][100], transpose[100][100];
    int i, j;

    // Step 2: Prompt the user to enter the dimensions of the matrix
    printf("Enter the number of rows and columns of the matrix: ");
    scanf("%d %d", &rows, &cols);

    // Step 3: Input the elements of the matrix
    printf("Enter elements of the matrix:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Step 4: Compute the transpose of the matrix
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Step 5: Display the transposed matrix
    printf("Transposed matrix:\n");
    for (i = 0; i < cols; i++) {
        for (j = 0; j < rows; j++) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }

    return 0;  // Step 6: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variables rows and cols store the dimensions of the matrix. matrix and transpose are 2D arrays to store the input matrix and the transposed matrix, respectively. The variables i and j are used as loop counters.

Step 2: Input the Dimensions of the Matrix

  • The program prompts the user to enter the number of rows and columns for the matrix using scanf.

Step 3: Input the Elements of the Matrix

  • The program uses nested for loops to take input for each element of the matrix from the user.

Step 4: Compute the Transpose of the Matrix

  • The program uses nested for loops to compute the transpose of the matrix:
    • Each element at position (i, j) in the original matrix is placed at position (j, i) in the transposed matrix.

Step 5: Display the Transposed Matrix

  • The program uses nested for loops to display the elements of the transposed matrix.

Step 6: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example:

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

Another Example:

Enter the number of rows and columns of the matrix: 3 2
Enter elements of the matrix:
7 8
9 10
11 12
Transposed matrix:
7 9 11
8 10 12

Conclusion

This C program demonstrates how to compute the transpose of a matrix by swapping rows with columns. It covers basic concepts such as arrays, loops, and matrix operations, making it a useful example for beginners learning C programming.

Leave a Comment

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

Scroll to Top