Data Structure Programs
|
Program to perform different functions on matrices like
addition, Subtraction and multiplication. |
//header files
#include<iostream.h>
#include<conio.h>
//function prototypes
void add();
void sub();
void multi();
void option();
void conti();
int a1[10][10],a2[10][10],r1,c1,r2,c2,i,j;
//main function
void main()
{
clrscr();
cout<<"\t\n ***PROGRAM to show different operations on 2-D
ARRAY***\n\n\n";
cout<<"Enter the number of rows for matrix 1 = ";
cin>>r1;
cout<<"\n\nEnter the number of columns for matrix 1 = ";
cin>>c1;
cout<<"\n\nEnter the element of first matrix\n";
for(i=1;i<=r1;i++)
{
cout<<"\n";
for(j=1;j<=c1;j++)
{
cout<<"\t";
cout<<"a1["<<i<<"]["<<j<<"] = ";
cin>>a1[i][j];
}
}
cout<<"\n\nEnter the number of rows for matrix 2 = ";
cin>>r2;
cout<<"\n\nEnter the number of columns for matrix 2 = ";
cin>>c2;
cout<<"\n\nEnter the elements of second matrix\n";
for(i=1;i<=r2;i++)
{
cout<<endl;
for(j=1;j<=c2;j++)
{
cout<<"\t";
cout<<"a2["<<i<<"]["<<j<<"] = ";
cin>>a2[i][j];
}
}
cout<<"\n\nDisplaying matrix 1\n\n";
for(i=1;i<=r1;i++)
{
cout<<"\n";
for(j=1;j<=c1;j++)
{
cout<<"\t";
cout<<a1[i][j];
}
}
cout<<"\n\nDisplaying matrix 2\n\n";
for(i=1;i<=r2;i++)
{
cout<<"\n";
for(j=1;j<=c2;j++)
{
cout<<"\t";
cout<<a2[i][j];
}
}
option();
conti();
getch();
}
//function for matrix additon
void add()
{
int s[10][10];
if(r1==r2&&c1==c2)
{
for(i=1;i<=r1;i++)
{
for(j=1;j<=c1;j++)
{
s[i][j]=a1[i][j]+a2[i][j];
}
}
cout<<"\n\nResult after addition\n\n";
for(i=1;i<=r1;i++)
{
cout<<endl;
for(j=1;j<=c1;j++)
{
cout<<"\t";
cout<<s[i][j];
}
}
}
else
{
cout<<"\n\nMatrices addition not possible.....";
}
}
//function for matrix
subtraction
void sub()
{
int s[10][10];
if(r1==r2&&c1==c2)
{
for(i=1;i<=r1;i++)
{
for(j=1;j<=c1;j++)
{
s[i][j]=a1[i][j]-a2[i][j];
}
}
cout<<"\n\nResult after subtraction\n\n";
for(i=1;i<=r1;i++)
{
cout<<endl;
for(j=1;j<=c1;j++)
{
cout<<"\t";
cout<<s[i][j];
}
}
}
else
{
cout<<"\n\nMatrices subtraction not possible.....";
}
}
//function for matrix multipliction
void multi()
{
int m[10][10];
if(c1==r2)
{
for(i=1;i<=r1;i++)
{
for(j=1;j<=c2;j++)
{
m[i][j]=0;
for(int k=1;k<=r2;k++)
{
m[i][j]+=a1[i][k]*a2[k][j];
}
}
}
cout<<"\n\nResult after multiplication\n";
for(i=1;i<=r1;i++)
{
cout<<endl;
for(j=1;j<=c2;j++)
{
cout<<"\t";
cout<<m[i][j];
}
}
}
else
{
cout<<"\nMultiplication not possible....";
}
}
//function for choosing desire operation
void option()
{
int ss;
cout<<"\n\n Choose the operation you want to apply\n\n";
cout<<" PRESS 1 for ADDITION\n PRESS 2 for SUBTRACTION\n PRESS 3
for MULTIPLICATION\n PRESS 4 for EXIT the program...";
cout<<"\n\nEnter your choice = ";
cin>>ss;
switch(ss)
{
case 1:
add();
break;
case 2:
sub();
break;
case 3:
multi();
break;
case 4:
break;
default:
break;
}
}
//function for continuing the program on user
basis
void conti()
{
int c;
cout<<"\n\nDo you want to proceed...\n";
cout<<"\n1) YES (proceeding with same matrix)\n2) NO\n3)
RESTART\n";
conti:
cout<<"\nEnter your choice = ";
cin>>c;
switch(c)
{
case 1:
option();
break;
case 2:
cout<<"\n\nEXITING program..... Press ENTER";
break;
case 3:
main();
break;
default:
cout<<"\nINVALID SELECTION";
goto conti;
}
}
|
Output: |

If any
of you want to submit your programs and
contribute to site please mail us at:
citysuvidha@gmail.com
Click here to have a look at List of programs
You can download all the programs in a zip file also. For this you have to do one thing. Just register at the citysuvidha forum and start any thread of any topic. Once you start the thread and put your request, admin will automatically send you the desired zip file.