Transpose of a matrix C program

It’s a transpose of a matrix C program that inputs numbers from the user and stores these numbers in two-dimensional arrays as to create a matrix and then find the transpose of it, and print matrix on the screen.

To understand this program, it’s better you should know the following topics:

Program (1): to create and print matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
//consider matrices A and B size as 3x3
int a[3][3],b[3][3];
int i,j;
//accepting values into an array a
printf("Accept elements in Matrix A\n");
for(i=0;i<3;i++)
{
     for(j=0;j<3;j++)
      {
       scanf("%d",&a[i][j]);
       }
 }
//to print array a elements
printf("\nThe Original Matrix A is\n");
for(i=0;i<3;i++)
{
     for(j=0;j<3;j++)
      {
       printf("%6d",a[i][j]);
       }
       printf("\n");
 }
 //to find array b elements
for(i=0;i<3;i++)
{
     for(j=0;j<3;j++)
      {
      b[i][j]=a[j][i];
       }
 }
//to print array b elements
printf("\nThe Transpose Matrix B is\n");
for(i=0;i<3;i++)
{
     for(j=0;j<3;j++)
      {
       printf("%6d",b[i][j]);
       }
       printf("\n");
 }
getch();

Output (1)

Accept elements in Matrix A
1
2
3
4
5
6
7
8
9

The Original Matrix A is
     1     2     3
     4     5     6
     7     8     9

The Transpose Matrix B is
     1     4     7
     2     5     8
     3     6     9

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *