0% found this document useful (0 votes)
20 views6 pages

Cycle 7

Uploaded by

ajoantony2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Cycle 7

Uploaded by

ajoantony2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CYCLE:07

1. Write a menu driven program for performing matrix addition,


multiplication and finding the transpose. Use functions to (i) read a
matrix, (ii) find the sum of two matrices, (iii) find the product of two
matrices, (iv) find the transpose of a matrix and (v) display a matrix.
https://onlinegdb.com/8g-9fMLhX

#include <stdio.h>

int main()
{
int i,j,a[10][10],b[10][10],sum[10][10],r,c,sub[10][20],mul[10][20],d[10]
[20],t1[10][10],t2[10][10],ch;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &r,&c);
printf("Enter the values of first array with %d elements: ", r*c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the values of second arraywith %d elements : ", r*c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf(" 1)Matrix Addition\n 2)Matrix Subtraction\n 3)Matrix
Multiplication\n 4)Transpose of a matrix\n 5)Display the Matrix\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
}
printf("Sum is:\n ");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", sum[i][j]);
}
printf("\n");
}
break;
case 2:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sub[i][j]=a[i][j]-b[i][j];
}
}
printf("difference is:\n ");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{

printf("%d\t", sub[i][j]);
}
printf("\n");
}
break;
case 3:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=a[i][j]*b[i][j];
}
}
printf("Multiplication is:\n ");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{

printf("%d\t", mul[i][j]);
}
printf("\n");
}
break;
case 4:
for (int i = 0; i < r; ++i)
{

for (int j = 0; j < c; ++j)


{
t1[j][i] = a[i][j];
}
}
printf("\nTranspose of the matrix1:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j)
{
printf("%d ", t1[i][j]);
if (j == r - 1)
printf("\n");
}
for (int i = 0; i < r; ++i)
{

for (int j = 0; j < c; ++j)


{
t2[j][i] = b[i][j];
}
}

printf("\nTranspose of the matrix2:\n");


for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j)
{
printf("%d ", t2[i][j]);
if (j == r - 1)
printf("\n");
}
break;
case 5:
printf("First Matrix 1:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("First Matrix 2:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", b[i][j]);
}
printf("\n");
}
break;
default:
printf("Inavalid choice");
}

return 0;
}

You might also like