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

Practical-5 and Practical-6 - C Language Code

The document discusses operations like addition, subtraction, transpose, trace and multiplication of square matrices. It includes the theory behind these operations and provides a C program to demonstrate adding, subtracting, transposing, taking the trace and multiplying matrices.

Uploaded by

Shivani Rawat
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 views6 pages

Practical-5 and Practical-6 - C Language Code

The document discusses operations like addition, subtraction, transpose, trace and multiplication of square matrices. It includes the theory behind these operations and provides a C program to demonstrate adding, subtracting, transposing, taking the trace and multiplying matrices.

Uploaded by

Shivani Rawat
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/ 6

Practical-5

Aim: Addition, Subtraction, Transpose, Trace and Multiplication of a square matrix N*N.
Objective: Write a program to enter two nxn (square) matrices from user and calculate its sum,
difference, transpose, trace and product.

Theory: To work on matrix, we need to declare two dimensional array.


Syntax: type array_name[row_size][column_size]

Two dimensional array take value from user using nested loop, where first loop represent rows
and second loop is for columns of matrix.
Sum: Sum of two matrix is calculated by adding each element of first matrix with
corresponding element of second matrix and sorting value in a new matrix.
Difference: Subtract each element of first matrix with corresponding element of first matrix
and sorting value in new matrix using nested loop.
Transpose: We store value of row elements of a matrix in a column of other new matrix

For e.g... If A = then AT =

Product: If we consider

AXB =

Trace: Sum of diagonal elements of a matrix is called trace.

A=
Trace = a + e + j

Code:

#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3],b[3][3],c[3][3],d[3][3],transpose[3][3],mult[3][3]={0,0},trace;
int i,j,k,l,m,n,v,f,g,h,u;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter%d%d element of a:",i,j);
scanf("%d",&a[i][j]);
printf("Enter%d%d element of b:",i,j);
scanf("%d",&b[i][j]);
}
printf("\n");
}
printf("Addition\n");
for(k=0;k<3;k++)
{
for(m=0;m<3;m++)
{
c[k][m]=a[k][m]+b[k][m];
printf("%d\t",c[k][m]);
}
printf("\n");
}
printf("Subtraction\n");
for(l=0;l<3;l++)
{
for(n=0;n<3;n++)
{
d[l][n]=a[l][n]-b[l][n];
printf("%d\t",d[l][n]);
}
printf("\n");
}
printf("Transpose\n");
for(v=0;v<3;v++)
{
for(f=0;f<3;f++)
{
transpose[v][f]=a[f][v];
printf("%d\t",transpose[v][f]);
}
printf("\n");
}
trace=a[0][0]+a[1][1]+a[2][2];
printf("Trace=%d\n",trace);
printf("Multiplication:\n");
for(u=0;u<3;u++)
{
for(g=0;g<3;g++)
{
for(h=0;h<3;h++)
{
mult[g][u]=mult[g][u]+(a[g][h]*b[h][g]);
}
printf("%d\t",mult[g][u]);
}
printf("\n");
}
}
Output: paste output here

Practical-6

Aim: Matrix Multiplication of N*M matrix


Objective: Write a program for multiplication of N*M matrix.
Theory: Same as in the practical-6
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{

int a1[10][10],a2[10][10],c[10][10],i,j,k,a,b,x,p,q;
printf("Enter the size of 1st matrix\n");
scanf("%d %d",&a,&b);
printf("Enter the size of 2st matrix\n");
scanf("%d %d",&p,&q);

printf("Enter element row wise\n ");


printf("Enter the element of 1st matrix\n");
for(i=0;i<a;i++)
{

for(j=0;j<b;j++)
{
scanf("%d",&a1[i][j]);
}
}
printf("Enter the element of 2nd matrix\n");
for(i=0;i<p;i++)
{

for(j=0;j<q;j++)
{
scanf("%d",&a2[i][j]);
}
}

printf("Entered matrix is\n\n");


for(i=0;i<a;i++)
{
printf("\n");
for(j=0;j<b;j++)
{
printf("%d ",a1[i][j]);
}
}
printf("\n");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d ",a2[i][j]);
}
}
printf("\nProduct of two matrix is \n");
for(i=0;i<a;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<b;k++)
{
c[i][j]=c[i][j]+a1[i][k]*a2[k][j];
}
}
}

for(i=0;i<a;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d ",c[i][j]);
}
}
}

Output: paste output here

You might also like