C Programming code for Transpose of 2D Matrix – KLE Tech

Write a Modular C Programming Code for Transpose of a 2D Matrix?

CODE:

#include<stdio.h>
#define ROW 10
#define COL 10

void ReadArray(int [ROW][COL], int, int);
void DisplayArray(int [ROW][COL], int, int);
void trans(int [ROW][COL],int [ROW][COL],int ,int) ;
main()
{
int r,c,sum;
int a[ROW][COL];
int b[ROW][COL];
printf("Enter the order of 2D matrix: ");
scanf("%d%d",&r,&c);
printf("\nEnter the Elements of Matix:\n");
ReadArray(a,r,c);
printf("\nThe Elements of Matix are\n");
DisplayArray(a,r,c);
trans(a,b,r,c);
}

void ReadArray(int a[ROW][COL], int r, int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
}

void DisplayArray(int a[ROW][COL], int r, int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
}

void trans(int a[ROW][COL], int b[ROW][COL], int r, int c)
{
int i,j ;
printf("Transpose of matrix: \n");
for (i=0;i<r;i++)
{
for (j=0;j<c;j++)
{
b[i][j]=a[j][i];
printf("\t%d",a[j][i]);
}printf("\n");
}
}

OUTPUT

Enter the order of 2D matrix: 3 3

Enter the Elements of Matix:
1
2
3
4
5
6
7
8
9

The Elements of Matix are
1 2 3
4 5 6
7 8 9
Transpose of matrix:
1 4 7
2 5 8
3 6 9

Process returned 0 (0x0) execution time : 7.933 s
Press any key to continue.

 

Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:

Top 100+ C Programming codes – KLE Technological University

 

Leave a Comment