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

Assignment 4

This document contains programs to implement matrix multiplication and addition in C. The matrix multiplication program prompts the user to enter the number of rows and columns of two matrices, then enters the elements of each matrix. It then calculates the product matrix using a nested for loop and prints the result. The matrix addition program follows a similar process, but adds the corresponding elements of the two matrices instead of multiplying them.

Uploaded by

Fulsoundar Parth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Assignment 4

This document contains programs to implement matrix multiplication and addition in C. The matrix multiplication program prompts the user to enter the number of rows and columns of two matrices, then enters the elements of each matrix. It then calculates the product matrix using a nested for loop and prints the result. The matrix addition program follows a similar process, but adds the corresponding elements of the two matrices instead of multiplying them.

Uploaded by

Fulsoundar Parth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 4

1) Write a program to implement multiplication of


matrices.

#include<stdio.h>
void main()
{
int p[50][50],q[50][50],multi[50][50],r,c,i,j,k;
printf("Enter the number of rows:");
scanf("%d",&r);
printf("Enter the number of columns:");
scanf("%d",&c);
printf("\n Enter the elements of first matrix:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“\n a%d%d=”,i+1,j+1);
scanf("%d",&p[i][j]);
}
}
printf("enter the element of second matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“\n a%d%d= “,i+1,j+1);
scanf("%d",&q[i][j]);
}
}
printf("multiplication of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
multi[i][j]=0;
for(k=0;k<c;k++)
{
multi[i][j]+=p[i][k]*q[k][j];
}}}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",multi[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows:3
Enter the number of columns:3
Enter the elements of first matrix:
a11=1
a12=1
a13=1
a21=2
a22=2
a23=2
a31=3
a32=3
a33=3
Enter the element of second matrix=
a11=1
a12=1
a13=1
a21=2
a22=2
a23=2
a31=3
a32=3
a33=3
multiplication of the matrix=
6 6 6
12 12 12
18 18 18

2) Write a program to implement Addition of matrices.


#include<stdio.h>
void main()
{
int a,b,i,j, p[50][50], q[50][50], sum[50][50];
printf("Enter the number of rows:");
scanf("%d",&a);
printf("Enter the number of columns:");
scanf("%d",&b);

printf("\n Enter the elements of first matrix:");


for(i=0;i<a;i++)
for(j=0;j<b;j++)
{
printf("\n Enter element a%d%d:",i+1,j+1);
scanf("%d",&p[i][j]);
}
printf("\n Enter elements of 2nd matrix:\n");
for (i = 0; i < a; i++)
for (j = 0; j < b; j++)
{
printf("\n Enter element b %d%d: ", i+1, j+1);
scanf("%d", &q[i][j]);
}
for(i=0;i<a;i++)
for(j=0;j<b;j++)
{
sum[i][j]=p[i][j]+q[i][j];
}

printf("\nSum of two matrices: \n");


for (i = 0; i < a; i++)
for (j = 0; j < b; j++)
{
printf("%d ", sum[i][j]);
if (j == b - 1) {
printf("\n\n");
}

}
}
Output:
Enter the number of rows:2
Enter the number of columns:3

Enter the elements of first matrix:


Enter element a11= 2
Enter element a12= 3
Enter element a13= 4
Enter element a21= 5
Enter element a22= 2
Enter element a23= 3

Enter elements of 2nd matrix:

Enter element b11= -4


Enter element b12= 5
Enter element b13= 3
Enter element b21= 5
Enter element b22= 6
Enter element b23= 3

Sum of two matrices:


-2 8 7

10 8 6

You might also like