ADVERTISEMENT
ADVERTISEMENT

C program to transpose a matrix

#include <stdio.h>
 
#define MAXROW      10
#define MAXCOL      10
 
int main()
{
    int matrix[MAXROW][MAXCOL];
    int i,j,r,c;
     
    printf("Enter number of Rows :");
    scanf("%d",&r);
    printf("Enter number of Cols :");
    scanf("%d",&c);
 
    printf("\nEnter matrix elements :\n");
    for(i=0;i< r;i++)
    {
        for(j=0;j< c;j++)
        {
            printf("Enter element [%d,%d] : ",i+1,j+1);
            scanf("%d",&matrix[i][j]);
        }
    }
 
    /*Transpose a matrix */
    printf("\nTranspose Matrix is :");
    for(i=0;i< c;i++)
    {
        for(j=0;j< r;j++)
        {
            printf("%d\t",matrix[j][i]);    /*print elements*/
        }
        printf("\n");   /*after each row print new line*/      
    }
    return 0;       
}

Output

    Enter number of Rows :2 
    Enter number of Cols :3 

    Enter matrix elements : 
    Enter element [1,1] : 1 
    Enter element [1,2] : 2 
    Enter element [1,3] : 3 
    Enter element [2,1] : 4 
    Enter element [2,2] : 5 
    Enter element [2,3] : 6 

    Transpose Matrix is :
    1	4	 
    2	5	 
    3	6

C Two-dimensional Arrays Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.