Write a program to find the transpose of the matrix using dynamic memory allocation.
C Program
#include <stdio.h>#include <stdlib.h>int main(){//declaring pointersint *tranMatrix;int row, col;printf("Enter rows and columns in the matrix: ");scanf("%d%d", &row, &col);//memory allocating for matrix using dynamic memory allocationtranMatrix = (int *)calloc(row * col, sizeof(int));printf("Enter the rows and column value in matrix format: \n");for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){scanf("%d", tranMatrix + (i * col + j) * sizeof(int));}}printf("Transpose of matrix is: \n");for (int i = 0; i < col; i++){for (int j = 0; j < row; j++){printf("%4d", *(tranMatrix + (j * col + i) * sizeof(int)));}printf("\n");}return 0;}
Input:
Enter rows and columns in the matrix: 2 3
Enter the rows and column value in matrix format:
2 3 4
5 4 2
Output:
Transpose of matrix is:
2 5
3 4
4 2
No comments:
Post a Comment