9.4.20

transpose matrix


TRANSPOSE MATRIX
#include <stdio.h>
void main()
{

    int m[5][5],t[5][5],i,j,r,c;
    printf("How many rows and cols");
    scanf("%d%d",&r,&c);
    printf("enter the matrix:");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        scanf("%d",&m[i][j]);
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        t[j][i]=m[i][j];
    printf("original matrix:\n");
    for(i=0;i<r;i++)
    {

        for(j=0;j<c;j++)
            printf("%5d",m[i][j]);
        printf("\n");

    }
printf("\n its transpose:\n");
for(i=0;i<c;i++)
{

    for(j=0;j<r;j++)
        printf("%5d",t[i][j]);
    printf("\n");
}
}
OUTPUT
How many rows and cols 2
2
Enter the matrix: 1
2
3
4
Orginal matrix:
1               2
2               4
Its transpose
1                 3
2                 4

No comments:

Post a Comment

JAVA program pattern 2

import java.util.Scanner; public class pattern3 { public static void main(String args[]) { int i,j; Scanner sc=new Scanner(System.in); Syste...