Matrices in C-language


Matrices:

Matrix Multiplication

 1#include<stdio.h>
 2int main()
 3{   int m,n,p,q,a[10][10],b[10][10],c[10][10],k,i,j;
 4    printf("enter the rows and column value for matrix 1:\n");
 5    scanf("%d%d",&m,&n);
 6    printf("enter the rows and columns value for matrix 2:\n");
 7    scanf("%d%d",&p,&q);
 8    printf("enter the elements of matrix 1:\n");
 9    for(i=0;i<m;i++)
10    {
11        for(j=0;j<n;j++){
12            scanf("%d",&a[i][j]);
13        }
14    }
15    printf("matrix 1 is:\n");
16    for(i=0;i<m;i++){
17        for(j=0;j<n;j++){
18            printf("%d\t",a[i][j]);
19        
20        }
21        printf("\n");
22    }
23    printf("enter the elements of matrix 2:\n");
24    for(i=0;i<p;i++)
25    {
26        for(j=0;j<q;j++){
27            scanf("%d",&b[i][j]);
28        }
29    }
30    printf("matrix 2 is:\n");
31    for(i=0;i<p;i++){
32        for(j=0;j<q;j++){
33            printf("%d\t",b[i][j]);
34        
35        }
36        printf("\n");
37    }
38    for(i=0;i<m;i++){
39        for(j=0;j<q;j++){
40            c[i][j]=0;
41        }
42    }
43
44    for(i=0;i<m;i++){
45        for(j=0;j<q;j++){
46            for(k=0;k<n;k++){
47                c[i][j]=c[i][j]+a[i][k]*b[k][j];
48
49            }
50        }
51    }
52    printf("\nmultiplication of the given two matrices is:\n");
53    for(i=0;i<m;i++){
54        for(j=0;j<q;j++){
55            printf("%d\t",c[i][j]);
56        }
57        printf("\n");
58    }
59
60    return 0;
61
62}

Transpose of a Matrix

 1#include <stdio.h>
 2int main()
 3{
 4    int m, i, j, n, a[10][10];
 5    printf("enter the size of rows and columns of the matrix\n");
 6    scanf("%d%d", &m, &n);
 7    printf("enter the elements of the matrix\n");
 8    for (i = 0; i < m; i++)
 9    {
10        for (j = 0; j < n; j++)
11        {
12            scanf("%d", &a[i][j]);
13        }
14    }
15    printf("the given matrix is\n");
16    for (i = 0; i < m; i++)
17    {
18        for (j = 0; j < n; j++)
19        {
20            printf("%d\t", a[i][j]);
21        }
22        printf("\n");
23    }
24    printf("the transpose of the given matrix is:\n");
25    for (i = 0; i < m; i++)
26    {
27        for (j = 0; j < n; j++)
28        {
29            printf("%d\t", a[j][i]);
30        }
31        printf("\n");
32    }
33    return 0;
34}