Matrix Operations using Function Template (C++)
April 24, 2016
Categorised in: C++ Codes, OOMP Codes
/* Write a program in C++ using function template to read two matrices of different data types such as integers and floating point values and perform simple arithmetic operations on these matrices separately and display it. */ #include<iostream> #include<stdlib.h> using namespace std; template <class matrix> void add(matrix mat1[20][20],int r1,int c1,matrix mat2[20][20],int r2,int c2) { if( r1==r2 && c1==c2) { cout<<"\nADDITION:\n"; for( int i=0;i<r1;i++) { for( int j=0;j<c1;j++) { cout<<mat1[i][j]+mat2[i][j]<<"\t"; } cout<<"\n"; } } else cout<<"\nAddition can’t be possible\n"; } template <class matrix> void sub(matrix mat1[20][20],int r1,int c1,matrix mat2[20][20],int r2,int c2) { if( r1==r2 && c1==c2) { cout<<"\nSUBTRACTION:\n"; for( int i=0;i<r1;i++) { for( int j=0;j<c1;j++) { cout<<mat1[i][j]-mat2[i][j]<<"\t"; } cout<<"\n"; } } else cout<<"\nSubtraction can’t be possible\n"; } template <class matrix> void mul(matrix mat1[20][20],int r1,int c1,matrix mat2[20][20],int r2,int c2) { matrix temp; if( r2==c1) { cout<<"\nMULTIPLICATION:\n"; for( int i=0;i<r1;i++) { for( int j=0;j<c2;j++) { temp=0; for( int k=0;k<c1;k++) { temp=temp+(mat1[i][k]*mat2[k][j]); } cout<<temp<<"\t"; } cout<<"\n"; } } else cout<<"\nMultiplication can’t be possible\n"; } template <class t> void print(t mat1[20][20],int r1,int c1,t mat2[20][20],int r2,int c2) { cout<<"\nMATRIX 1:\n"; for( int i=0;i<r1;i++) { for( int j=0;j<c1;j++) { cout<<mat1[i][j]<<"\t"; } cout<<"\n"; } cout<<"\nMATRIX 2:\n"; for( int i=0;i<r2;i++) { for( int j=0;j<c2;j++) { cout<<mat2[i][j]<<"\t"; } cout<<"\n"; } } template <class m> void getdata(m mat1[20][20],int r1,int c1,m mat2[20][20],int r2,int c2) { cout<<"\nMATRIX 1:\n"; for( int i=0;i<r1;i++) { for( int j=0;j<c1;j++) { cout<<"Enter the data for a["<<i<<"]["<<j<<"]\t"; cin>>mat1[i][j]; } } cout<<"\nMATRIX 2:\n"; for( int i=0;i<r2;i++) { for( int j=0;j<c2;j++) { cout<<"Enter the data for a["<<i<<"]["<<j<<"]\t"; cin>>mat2[i][j]; } } } int main() { int choice; int a[20][20],b[20][20]; float c[20][20],d[20][20]; int r1,r2,c1,c2; while(1) { cout<<"\n\n************** WELCOME TO MATRIX CALCULATOR *****************\n\t1)INPUT\n\t2)DISPLAY\n\t3)ADDITION\n\t" <<"4)SUBTRACTION\n\t5)MULTIPLICATION\n\t6)EXIT\n\nEnter your choice\t"; cin>>choice; switch(choice) { case 1: cout<<"\nMATRIX 1:\n Enter number of row\t"; cin>>r1; cout<<"\nEnter number of column\t"; cin>>c1; cout<<"\nMATRIX 2:\n Enter number of row\t"; cin>>r2; cout<<"\nEnter number of column\t"; cin>>c2; getdata(a,r1,c1,b,r2,c2); break; case 2: print(a,r1,c1,b,r2,c2); break; case 3: add(a,r1,c1,b,r2,c2); break; case 4: sub(a,r1,c1,b,r2,c2); break; case 5: mul(a,r1,c1,b,r2,c2); break; case 6: exit(0); break; default: cout<<"\nyou have enter wrong choice plz try again"; } } }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.