Program to find transpose of a matrix in C


Program to find transpose of a matrix in C
·         To understand this C program you should have knowledge of Two dimensional array.
Two-dimensional array:  Two dimensional array are represented in the form of matrix and tables. The arrays have two subscript: one represents the row and other represent the columns.
The transpose of a matrix is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns.
Program:
#include <stdio.h>
#include <conio.h>
void transpose (int [] [10], int, int);
void display (int [] [10], int, int);
void main ()
{
int a[10] [10];
int I, j, rows, cols;
clrscr ();
printf (“Enter no. of rows in a matrix(<10): “);
scanf (“%d”, &rows);
printf (“ Enter no. of columns in the matrix (<10) :”);
scanf (“%d”, &cols);
printf (“ - - - - - Enter row wise %d integer of matrix - - - - - \n”, rows, cols);
for (i=0; i<rows; i++)
for (j=0; j<cols; j++)
{
scanf (“%d”, &a[i] [j] );
}
display (a, rows, cols);
transpose (a, rows, cols);
getch ();
}
void transpose ( int x[] [10], int p, int q)
{
int i, j, k;
int  y[10] [10];
for (i=0; i<p; i++)
for (j=0; j<q; j++)
{
y [i] [j] = x [i] [j];
}
printf (“\n - - - - - Transpose is - - - - -“);
display (y, q, p);
}
void display (int c[] [10], int m, int n)
{
int i, j;
printf (“ \n - - - - - Displaying %d by %d data - - - - - \n”, m, n);
for (i=0; i<m; i++)
{
for ( j=0; j<n; j++)
printf (“%d\t”, c[i] [j]);
printf (“\n”);
}
}
OUTPUT:
Enter no of rows in the matrix (<10): 2
Enter no. of columns in the matrix (<10) :3
- - - - - Enter row wise %d integer of matrix - - - - -
2   3   4   55   66   77
- - - - - Displaying 2 by 3 data - - - - -
2      3      4     
55      66      77
- - - - - Transpose is - - - - -
- - - - - Displaying 3 by 2 data - - - - -
2      55
3      66
4      77

Explanation of program:
The transpose of matrix is calculated by interchanging rows and columns of the matrix .
Tips To Remember:
1. The transpose of the matrix is defined as the matrix that is obtained by interchanging the rows of a matrix to columns. If the matrix is of the order m*n then transpose of the matrix will be n*m.
2. In matrix multiplication, the number of columns in the first matrix should be equal to the number of rows in the second matrix. Each row of the first matrix is multiplied with the columns of the second matrix then added to get the element of the result.
     
 Detailed about array in C. click here

No comments

Powered by Blogger.