Introduction
In this lab, you will learn how to compute the transpose of a matrix in C. The lab covers the following steps:
Read Dimensions and Elements: You will learn how to read the dimensions (rows and columns) of a matrix and populate it with user-input values. This step sets up the foundation for the matrix transposition process.
Swap Rows and Columns: The core of the matrix transposition involves swapping the rows and columns of the original matrix to create the transposed matrix.
Print the Transposed Matrix: Finally, you will learn how to display the transposed matrix, allowing you to verify the correctness of the transposition process.
By the end of this lab, you will have a solid understanding of matrix transposition and the ability to implement it in C programming.
Read Dimensions and Elements
In this step, you'll learn how to read matrix dimensions and elements in a C program for matrix transposition. We'll create a program that allows users to input the number of rows and columns, and then populate the matrix with values.
First, let's create a new C file for our matrix transpose program:
cd ~/project
nano matrix_transpose.c
Now, add the following code to read matrix dimensions and elements:
#include <stdio.h>
#define MAX_ROWS 100
#define MAX_COLS 100
int main() {
int rows, cols;
int matrix[MAX_ROWS][MAX_COLS];
// Read matrix dimensions
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
// Input matrix elements
printf("Enter matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
// Print original matrix
printf("\nOriginal Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Compile and run the program:
gcc matrix_transpose.c -o matrix_transpose
./matrix_transpose
Example output:
Enter number of rows: 3
Enter number of columns: 3
Enter matrix elements:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 4
Enter element [1][1]: 5
Enter element [1][2]: 6
Enter element [2][0]: 7
Enter element [2][1]: 8
Enter element [2][2]: 9
Original Matrix:
1 2 3
4 5 6
7 8 9
Let's break down the key parts of the code:
- We define maximum matrix size using
MAX_ROWSandMAX_COLS scanf()is used to read user input for rows, columns, and matrix elements- Nested loops are used to input and display matrix elements
- The program validates input within the maximum allowed matrix size
Swap Rows and Columns
In this step, you'll learn how to transpose a matrix by swapping its rows and columns. We'll modify the previous program to create a transposed matrix.
Open the existing matrix_transpose.c file:
cd ~/project
nano matrix_transpose.c
Replace the previous code with the following implementation:
#include <stdio.h>
#define MAX_ROWS 100
#define MAX_COLS 100
int main() {
int rows, cols;
int matrix[MAX_ROWS][MAX_COLS];
int transposed[MAX_COLS][MAX_ROWS];
// Read matrix dimensions
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
// Input matrix elements
printf("Enter matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
// Transpose the matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed[j][i] = matrix[i][j];
}
}
// Print original matrix
printf("\nOriginal Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Print transposed matrix
printf("\nTransposed Matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", transposed[i][j]);
}
printf("\n");
}
return 0;
}
Compile and run the program:
gcc matrix_transpose.c -o matrix_transpose
./matrix_transpose
Example output:
Enter number of rows: 3
Enter number of columns: 3
Enter matrix elements:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 4
Enter element [1][1]: 5
Enter element [1][2]: 6
Enter element [2][0]: 7
Enter element [2][1]: 8
Enter element [2][2]: 9
Original Matrix:
1 2 3
4 5 6
7 8 9
Transposed Matrix:
1 4 7
2 5 8
3 6 9
Key points about matrix transposition:
- We create a new matrix
transposedwith swapped dimensions - The transposition is done by swapping rows and columns
transposed[j][i] = matrix[i][j]is the key transformation- The dimensions of the original and transposed matrices are reversed
Print the Transposed Matrix
In this final step, you'll learn how to format and print the transposed matrix with improved readability and error handling. We'll enhance the previous program to make the output more professional.
Open the matrix_transpose.c file:
cd ~/project
nano matrix_transpose.c
Replace the previous code with the following improved implementation:
#include <stdio.h>
#define MAX_ROWS 100
#define MAX_COLS 100
void printMatrix(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols, const char* title) {
printf("%s:\n", title);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%4d ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}
int main() {
int rows, cols;
int matrix[MAX_ROWS][MAX_COLS];
int transposed[MAX_COLS][MAX_ROWS];
// Input validation
do {
printf("Enter number of rows (1-%d): ", MAX_ROWS);
scanf("%d", &rows);
} while (rows <= 0 || rows > MAX_ROWS);
do {
printf("Enter number of columns (1-%d): ", MAX_COLS);
scanf("%d", &cols);
} while (cols <= 0 || cols > MAX_COLS);
// Input matrix elements
printf("Enter matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
// Transpose the matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed[j][i] = matrix[i][j];
}
}
// Print matrices with formatted output
printMatrix(matrix, rows, cols, "Original Matrix");
printMatrix(transposed, cols, rows, "Transposed Matrix");
return 0;
}
Compile and run the program:
gcc matrix_transpose.c -o matrix_transpose
./matrix_transpose
Example output:
Enter number of rows (1-100): 3
Enter number of columns (1-100): 3
Enter matrix elements:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 4
Enter element [1][1]: 5
Enter element [1][2]: 6
Enter element [2][0]: 7
Enter element [2][1]: 8
Enter element [2][2]: 9
Original Matrix:
1 2 3
4 5 6
7 8 9
Transposed Matrix:
1 4 7
2 5 8
3 6 9
Key improvements in this version:
- Added input validation for matrix dimensions
- Created a separate
printMatrix()function for better code organization - Improved matrix printing with aligned formatting using
%4d - Added titles to matrix output for clarity
- Handles different matrix sizes more robustly
Summary
In this lab, you will learn how to read matrix dimensions and elements, swap rows and columns, and print the transposed matrix in a C program. First, the program prompts the user to enter the number of rows and columns, and then allows the user to input the matrix elements. The original matrix is then displayed. Next, the program swaps the rows and columns to compute the transpose of the matrix, and finally, the transposed matrix is printed.
The key learning points in this lab include using scanf() to read user input, implementing nested loops to input and display matrix elements, and performing the transpose operation by swapping rows and columns. The program also demonstrates the use of printf() to display the original and transposed matrices.



