You are on page 1of 10

Case Study for Matrix Operations

Defination :- Matrix Operations using Pointer

/* Matrix operation using pointer*/

#include<stdio.h>
#include<conio.h>

void add(int [3][3],int [3][3],int [3][3]);


void sub(int [3][3],int [3][3],int [3][3]);
void divi(int [3][3],int [3][3],int [3][3]);
void mul(int [3][3],int [3][3],int [3][3]);
void display(int [3][3],int [3][3],int[3][3]);

void main()
{
int mat1[3][3],mat2[3][3],ans[3][3];
int i,j,ch,ch1;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter value for a[%d][%d]",i,j);
scanf("%d",&mat1[i][j]);

}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter value for b[%d][%d]",i,j);
scanf("%d",&mat2[i][j]);
}
}
begin:
printf("\n Press 1 for Addition:- ");
printf("\n Press 2 for Substraction:-");
printf("\n Press 3 for Division:-");

printf("\n Press 4 for multiplication:-");


printf("\n Press 5 for Exit:-");
printf("\n\n Enter your choice:-");
scanf("%d",&ch);
switch(ch)
{
case 1:
add(mat1,mat2,ans);
printf("\n Addition of two Matrix\n");
display(mat1,mat2,ans);
break;
case 2:
sub(mat1,mat2,ans);
printf("\n Substraction of two Matrix\n");
display(mat1,mat2,ans);
break;
case 3:
divi(mat1,mat2,ans);
printf("\n Division of two Matrix \n");
display(mat1,mat2,ans);
break;
case 4:
mul(mat1,mat2,ans);
printf("\n Multiplication of two matrix \n");
display(mat1,mat2,ans);
break;
case 5:

exit(1);
default:
printf("please enter valid choice...");
getch();
goto begin;
}
printf("Press 1 for perform another operation...");
scanf("%d",&ch1);
if(ch1==1)
goto begin;
printf("Exit");
getch();
}
void add(int mat1[3][3],int mat2[3][3],int ans[3][3])
{
int i,j,*p;
p=ans;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
*p=mat1[i][j]+mat2[i][j];
p++;
}
}

void sub(int mat1[3][3],int mat2[3][3],int ans[3][3])


{
int i,j,*p;
p=ans;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{

*p=mat1[i][j]-mat2[i][j];
p++;
}
}

}
void divi(int mat1[3][3],int mat2[3][3],int ans[3][3])
{
int i,j,*p;
p=ans;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{

*p=mat1[i][j] / mat2[i][j];
p++;
}

}
}
void mul(int mat1[3][3],int mat2[3][3],int ans[3][3])
{
int i,j,k,*p,sum;
p=ans;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum=sum+mat1[i][k]*mat2[k][j];
}
*p=sum;
p++;
}
}
}
void display(int mat1[3][3],int mat2[3][3],int ans[3][3])
{
int i,j;
printf("\n First matrix :-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)

{
printf("%2d",mat1[i][j]);

}
printf("\n");

}
printf("\n Second matrix :-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%2d",mat2[i][j]);
}
printf("\n");
}
printf("\n Ansewer is :-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%2d",ans[i][j]);

}
printf("\n");

printf("\n");
}

Output of Program..
Press 1 for Addition:Press 2 for Substraction:Press 3 for Division:Press 4 for multiplication:Press 5 for Exit from Program:-

Enter your choice:Addition of two Matrix


First matrix :358
9 12 15
18 21 24
Second matrix :246
8 10 12
14 16 18
Ansewer is :-

5 9 14
17 22 27
32 37 42

Press 1 for perform another operation...1

Subtraction of two Matrix


First matrix :358
9 12 15
18 21 24
Second matrix :246
8 10 12
14 16 18

Ansewer is :112
123
456
Press 1 for perform another operation...5
Exit

You might also like