Arrays 221
{
in t i, j,rcw,col,a[10] [10];
clrscrO ;
printf ("tt Enter Order of matrix up to(10x 10) A:");
scanf (%d %d",&row,txcol};
printf ("Enter Elements of matrix A : ft");
for (i=0,i<rowrf++)
for (j=0,mj<col,j++)
scanf ("%d",&a[i][j]);
printf ("n The Matrix is: ");
for (i=$,'i<row,'i++)
I
for (j=0,j<col,-j++)
printf C%6d",a[i][fl);
printf ("n");
}
QUTPUX:
Enter Order of matrix up to (10 x 10) A : 3 3
Enter Elements of matrix A :
3 5 8
4 8 5
8 5 4
The Matrix is:
3 5 8
4 8 5
8 5 4
Explanation In the above program the order of the matrix is entered. The order of the matrix should
be up to 10 x 10. For the sake of understanding we have taken 3 x 3 matrix. Using first two nested
fo r loops with respect to rows and columns the elements of matrix are entered. The last two nested for
loops are used for displaying the matrix elements with respect to row and column.
Transpose of the matrix The Transpose of matrix interchanges rows & columns, i.e. The row
elements become column elements and vice versa.
7.28 Read the elements of the matrix of the order up to 10 x 10 and transpose its elements.
# include <stdio.h>
# include <conio.h>
void main ()
{
int i.j,rcwl,row2,coll,col2,a[10][10],b[10][10];
clrscrO ;
printf (" Enter Order of matrix up to (10X10) A :");
222 Programming and Data Structures
scanf("%d %d"r&trowlf&coll);
printf("Enter Elements of matrix A : ");
for (i=0,'i<rowl,i++)
for (j=0,j<coll,j++)
scanf ("% d",&ca[i][j]);
}
row2-coll;
col2=rowl;
for (i=0,'i<rowl,'i++)
I
for (j=0,-j<coll,-j++)
b[j][i]=a[i][j];
}
printf( The Matrix Transpose is ");
for (i=0,n<row2;i++)
{
for (j-0,'j<col2;j++)
prin1f(“%4d",b[i][j));
printf (" ");
I
QUJPUT.;
Enter Order of matrix up to (10 x 10) A : 3 3
Enter Elements of matrix A :
3 5 8
4 8 5
8 5 4
The Matrix is:
3 4 8
5 8 5
8 5 4
Explanation This program is same as last one. The difference between them is that the first program
displays the elements in the order in which they are entered. But in this program row and column
elements a re interchanged. This is obtained by interchanging the order of matrix through the
statements row2=coll & col2=rowl.
7.29 Read the elements of the matrix of the order up to 3 x 3 and transpose its elements.
#include<8tdio.h>
#include<conio.h>
main ()
{
int mat [3] [3] ;
int i,j;
Arrays 223
clrscr();
printf ("Enter Elements ofMatrixn");
for (i=0,H<3,'i++)
I
for (j=0,j<3;j++)
I
scanf ("%d",&mat[i][j]);
I
}
printf ("Transpose of the matrix ");
fo r(i=0;i <3,~i++)
{
for(/=0;j<3,j++)
I
printf(" %d",mat[j]li]);
I
printf("n");
I
getcheO;
I
OUTPUT:
Enter Elements of Matrix:
5 8 9
2 5 4
7 5 2
Transpose of the matrix
5 2 7
8 5 5
9 4 2
Explanation In the previous example, the order of the matrix was changed and the transpose of the
matrix was obtained. In this example, the la s tp rin tf () statement is used in which the two arguments
(mat [ j ] [ i ]) of matrix are interchanged and displayed.
7.30 Write a program to perform addition and subtraction of two matrices whose orders are
up to 10 x 10.
#include <stdio.h>
main()
{
int i,j,rl,cl,a[10][10],b[10][10];
clrscr();
printf ("Enter Order of Matrix A &BuptolOX 10:");
scanf ("%d %d",
printf ("Enter Elements of Matrix of A : ");
224 Programming and Data Structures
for (i=0;i<rl;i++)
I
for (j=0,-j<cl,j++)
scanf ("%d",&ax[i][j]);
I
printf ("Enter Elements of Matrix ofB : ");
for (i=0,~i<rli++)
I
for (j=0;j<cl,-j++)
scanf (“%d",kb[i][j]);
I
printf ("nMatrix Addition n");
for (i=0,-i<rl,-i++)
{
for (j=0,-j<cl;j++)
printf (“%5d”,a[i]lj]+b[i][j]);
printf rn");
I
printf ("nMatrix Subtraction n");
for (i=0,'i<rl,-i++)
I
for (j=0,j<clj++)
printf ("%5d",a[i][j]-b[iJ[j]);
printf rn");
)
getchO;
)
OUTPUT:
Enter Order of Matrix A & B up to 10 x 10:3 3
Enter Elements of Matrix of A :
4 5 8
2 9 8
2 9 4
Enter Elements of Matrix of B :
1 3 5
0 5 4
6 7 2
Arrays 225
Matrix Addition
5 8 13
2 13 12
816 6
Matrix Subtraction
3 2 3
2 4 4
-4 2 2
Explanation The elements of two matrices are read in the same manner as descrioed in the previous
examples. Addition and subtraction are computed and the results are displayed. Addition of
correspondingelementsofAandBmatricesareperformedbythestatementa[i] [j]+b[i] [ j ] ) .Similarly,
for subtraction a [i] [j] -b[i] [j]) statement is used.
7.31 Write a program to perform Multiplication of two matrices whose orders are up to
10x10.
#include <stdio.h>
mainO
{
int i,j,rl,cl, a[10][10],b[10][10];
clrscrO;
printf ("Enter Order of Matrix A&BuptolOx 10:");
scanf ("%d %d", &rl,&cl);
printf ("Enter Elements of Matrix of A : ”);
for (i=0,~i<rl;i++)
I
for (j=0;j<cl;j++)
scanf ("%d"Aali]ljl);
I
printf ("Enter Elements of Matrix ofB : ");
for (i-0;i<rl,i++)
{
for (j=0;j<cl;j++)
scanf ("%d",&b[i][j]);
I
printf (n Matrix Multiplication n");
for (i=0,-i<rl,-i++)
for (j=0,-j<cl;j++)
printf ("%5d",a[i][j]*b[i][j]);
printf (" ");
I
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.200.3