0% found this document useful (0 votes)
36 views

Code For Matrix Algebra

This document defines functions for matrix operations in C including: - Displaying a matrix - Adding two matrices - Multiplying two matrices - Finding the transpose of a matrix - Calculating row and column sums of a matrix The main function tests these operations by inputting sample matrices, performing the defined operations, and printing outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Code For Matrix Algebra

This document defines functions for matrix operations in C including: - Displaying a matrix - Adding two matrices - Multiplying two matrices - Finding the transpose of a matrix - Calculating row and column sums of a matrix The main function tests these operations by inputting sample matrices, performing the defined operations, and printing outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 #include <stdio.

h>
2 #include <stdlib.h>
3
4 void Display(int m,int n,int arr[][n]);
5 void Sum(int m,int n,int p,int q,int a[][n],int b[][q]);
6 void Multiply(int m,int n,int p,int q,int matrix1[][n],int matrix2[][
q]);
7 void Transpose(int m,int n,int arr[][n]);
8 void Nsum(int m,int n,int arr[][n]);
9
10 int main()
11 {
12 int m,n,p,q,i,j;
13 printf("Enter the dimensions of array 1: ");
14 scanf("%d %d",&m,&n);
15 printf("\nEnter the dimensions of array 2: ");
16 scanf("%d %d",&p,&q);
17 int arr1[m][n],arr2[p][q];
18 printf("\nEnter the elements of first array:");
19 for(i=0;i<m;i++){
20 for(j=0;j<n;j++)
21 scanf("%d",&arr1[i][j]);
22 }
23 printf("\nEnter the elements of second array:");
24 for(i=0;i<p;i++){
25 for(j=0;j<q;j++)
26 scanf("%d",&arr2[i][j]);
27 }
28 Sum(m,n,p,q,arr1,arr2);
29 Multiply(m,n,p,q,arr1,arr2);
30 Transpose(m,n,arr1);
31 Transpose(p,q,arr2);
32 Nsum(m,n,arr1);
33 Nsum(p,q,arr2);
34 return 0;
35 }
36 ////////////////////////////
37
38 void Display(int m,int n,int arr[][n]){
39 int i,j;
40 for(i=0;i<m;i++){
41 for(j=0;j<n;j++)
42 printf("%d\t",arr[i][j]);
43 printf("\n");
44 }
45 printf("\n");
46 }
47
48 void Sum(int m,int n,int p,int q,int a[][n],int b[][q]){
49 int res[m][n],i,j;
50 if( m!=p || n!=q)
51 printf("Number of rows and columns in a matrix must be equal for
addition");
52 else{
53 for(i=0;i<m;i++){
54 for(j=0;j<n;j++)
55 res[i][j]=a[i][j]+b[i][j];
56 }
57 printf("Sum of the two matrices is: \n");
58 Display(m,n,res);
59 }
60 }
61
62 void Multiply(int m,int n,int p,int q,int matrix1[][n],int matrix2[][
q]){
63 int i,j,k,matrix3[m][q],product;
64 if(n!=p)
65 printf("Number of columns in the first matrix must match the
number of rows in the second matrix");
66 else{
67 for(i=0;i<n;i++){
68 for(j=0;j<n;j++){
69 product = 0;
70 for(k=0;k<n;k++){
71 product = product + matrix1[i][k]*matrix2[k][j];
72 }
73 matrix3[i][j]=product;
74 }
75 }
76 printf("Multiplication of the two matrices is: \n");
77 Display(m,n,matrix3);
78 }
79 }
80
81 void Transpose(int m,int n,int arr[][n]){
82 printf("\n");
83 Display(m,n,arr);
84 int i,j,res[n][m];
85 for(i=0;i<m;i++){
86 for(j=0;j<n;j++){
87 res[j][i]=arr[i][j];
88 }
89 }
90 printf("Transpose of the above matrix is: \n");
91 Display(m,n,res);
92 }
93
94 void Nsum(int m,int n,int arr[][n]){
95 int j,i,sum;
96 for(i=0;i<m;i++){
97 sum = 0;
98 for(j=0;j<n;j++){
99 sum = sum+arr[i][j];
100 }
101 printf("Sum of all elements in row %d is: %d\n",i+1,sum);
102 }
103 for(i=0;i<m;i++){
104 sum = 0;
105 for(j=0;j<n;j++){
106 sum = sum+arr[j][i];
107 }
108 printf("Sum of all elements in column %d is: %d\n",i+1,sum);
109 }
110 }
111

You might also like