3. Write a C language program that dynamically allocates memory for a 2D array and calculates the transpose of the matrix.
Purpose
The motive of this c program is to illustrate dynamic memory allocation for a 2D array and compute its transpose.
Objective
Allocate memory dynamically for a 2D array primarily based on user input for dimensions.
To compute the transpose of the matrix in which columns becomes rows and vice versa.
To illustrate memory control, pointer utilization and matrix operations in C programming.
Real-Life Applications
Understanding and computing matrix transposes have sensible applications
Mathematics and Computer Science
Transposes are utilized in matrix manipulations like multiplication, fixing equations and finding determinants.
Matrices and their transposes are vital in algorithms for graph traversal and shortest path calculations.
Computer Graphics
Transposes play a function in transforming objects in 3D graphics.
Matrices are used in mapping item coordinates to display coordinates.
Data Analysis
Transposes are utilized in statistical computations and records manipulation.
Matrices and transposes are essential in machine learning gaining knowledge of algorithms for statistics pre-processing and featurenfunction extraction.
C program that dynamically allocates memory for a 2D array and calculates its transpose without using functions
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rows, cols;
int **matrix, **transpose;
// Input dimensions of the matrix
printf("Enter number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
// Allocate memory for the matrix (array of rows)
matrix = (int **)malloc(rows * sizeof(int *));
if (matrix == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Allocate memory for each row
for (int i = 0; i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
if (matrix[i] == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
}
// Input elements into the matrix
printf("Enter elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Print the original matrix
printf("The original matrix is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Allocate memory for the transpose matrix
transpose = (int **)malloc(cols * sizeof(int *));
if (transpose == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Allocate memory for each row of the transpose matrix
for (int i = 0; i < cols; i++) {
transpose[i] = (int *)malloc(rows * sizeof(int));
if (transpose[i] == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
}
// Compute the transpose of the matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Print the transpose matrix
printf("The transpose of the matrix is:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
// Free allocated memory for both matrices
for (int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
for (int i = 0; i < cols; i++) {
free(transpose[i]);
}
free(transpose);
return 0;
}
Output
Enter number of rows and columns of the matrix
3
3
Enter elements of the matrix
1 2 3
4 5 6
7 8 9
The transpose of the matrix is
1 4 7
2 5 8
3 6 9
Program Explanation
1.The code dynamically allocates memory for a matrix with the help of malloc() function.
2.It reads an entered matrix from the user or consumer.
3.It then perform the transpose of a entered matrix.
4.The transpose matrix is then shown on console to the user using printf().
5.Finally to avoid memory leaks problem the dynamically allotted memory is freed using free() function.
Previous :-->> 2. Write a program in C that dynamically allocates memory for a string and reverses the string in place.
-->> NEXT:
4. Write a C program that dynamically allocates memory for a linked list and implements a function to reverse the list.
-->>ALL
Dynamic Memory Allocation assignments in c