# Program in C to transpose a 2D array

Transpose means changing the rows of 2D array into columns and columns into rows. Below is the code in the C programming language to transpose a two dimensional array –

```/* C program to transpose the 2D array */

#include<stdio.h>

void main()
{
int a[10][10], b[10][10], m, n, i, j;

printf("\nEnter number of rows & columns of aray : ");
scanf("%d %d", &m, &n);

printf("\nEnter elements of 2-D array:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}
}

printf("\n\n2-D array before transposing:\n\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("\t%d", a[i][j]);
}
printf("\n\n");
}

/* Transposing array */
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
b[j][i] = a[i][j];
}
}

printf("\n\n2-D array after transposing:\n\n");
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
printf("\t%d", b[i][j]);
}
printf("\n\n");
}
getch();
}```

We create the 2D array first and after that it’s transpose is done which is stored in new 2D array. Hence the result is printed.
Input-
Enter number of rows & columns of aray : 3 x 3
Enter elements of 2-D array:
25   12   4   62   34   23   6   4   3

Output-
2-D array before transposing:

25   12  4

62   34  23

6     4     3

2-D array after transposing:

25   62   6

12   34   4

4    23    3

#### 9 thoughts on “Program in C to transpose a 2D array”

1. ajdfs says:

hahaaahhahahahaahha

1. Lakesha says:

Your answer was just what I neddee. It’s made my day!

2. CG: "Abrams specifies that there is no conflict between adhering to the Abrahamic covenant and being loyal to one's country."The fact that Abrams "specifies" anything doesn't make it so.CG: "Indeed, I would add that many very traditional Jews are fiercely loyal to the United States precisely because it gives them the freedom to pursue their unique traditions. . . " Like, for instance, whenever there's a conflict between Israel's interests and American well being, invariably putting Israel first? Some tradition.

3. wow this recipe looks great!can I use food colouring instead of powdered colouringand what exactly is ganache and can you make vanilla ganachesorry for all the questions i just want them to turn out good!thank you Ann!

2. awan says:

awesome work!!!

3. C program says:

The transpose of matrix A is written AT.
The ith row, jth column element of A is the jth row, ith column element of AT. If A is an m × n matrix then AT is an n × m matrix.

4. Amina Ashfaq says:

What code for 2d pointer programwith function array’s which find transpose of matrix?