Matrix operations in c language
Simple matrix operations in c language with Source Code for performing Matrix Operations in C and C++ Language . Operations on matrix which have to perform on matrix are as follows :
- Input Operations
- Display Operations
- Addition Operations
- Difference Operations
- Multiplications Operations
- Magic Square Operations
- Saddle Point Operations
- Transpose of Matrix
Above matrix operations in c language operations on matrix are divided into two categories as …..
- Without Pointers
- With Pointers
Matrix operations in c language Program Code
#include #include #define max 25 void input_p(int*,int*,int*); void display_p(int*,int,int); void add_p(int(*)[max],int(*)[max],int(*)[max],int,int); void difference_p(int(*)[max],int(*)[max],int(*)[max],int,int); void mul_p(int[][max],int[][max],int[][max],int,int,int); int ms_p(int[][max],int); int sp_p(int [][max],int,int); void transpose_p(int a[][max],int m); void input(int[][max],int*,int*); void display(int[][max],int,int); void add(int[][max],int[][max],int[][max],int,int); void difference(int[][max],int[][max],int[][max],int,int); void mul(int[][max],int[][max],int[][max],int,int,int); int ms(int[][max],int); int sp(int [][max],int,int); void transpose(int a[][max],int m); void withoutp(int [][max],int[][max],int*,int*,int*,int*); void withp(int [][max],int[][max],int*,int*,int*,int*); void main() { int a[max][max],b[max][max],c[max][max],r1,c1,r2,c2,ch; r1=r2=c1=c2=0; do { clrscr(); flushall(); printf("\n\n\n\t\t>>>>---------- MAIN MENU ----------<<<<<\n\n"); printf("\n\t1\tWITH POINTERS\n"); printf("\n\t2\tWITHOUT POINTERS\n"); printf("\n\t3\tEXIT"); printf("\n\n\n\tENTER YOUR CHOICE\t"); scanf("%c",&ch); switch(ch) { case '1': withp(a,b,&r1,&c1,&r2,&c2); break; case '2': withoutp(a,b,&r1,&c1,&r2,&c2); break; case '3': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch!='3'); getch(); } //end of main /****************************************************************** WITHP FUNCTION ********************************************************************/ void withp(int a[][max],int b[][max],int* r1,int* c1,int* r2,int* c2) { int c[max][max],m; char ch,ch1; do { flushall(); clrscr(); printf("\n\n\t===>>>\tWITHOUT POINTERS MENU\t<<<===\n"); printf("\n\t1\tINPUT\n"); printf("\t2\tDISPLAY\n"); printf("\t3\tADDITION\n"); printf("\t4\tDIFFERENCE\n"); printf("\t5\tMULTIPLICATION\n"); printf("\t6\tMAGIC SQUARE\n"); printf("\t7\tSADDLE POINT\n"); printf("\t8\tTRANSPOSE\n"); printf("\t9\tRETURN TO MAIN MENU\n"); printf("\n\tENTER YOUR CHOICE\t:\t"); scanf("%c",&ch); switch(ch) { case '1': do { flushall(); clrscr(); printf("\n\t>>>>---- INPUT MENU ----<<<<\n"); printf("\t1\tMATRIX A\n"); printf("\t2\tMATRIX B\n"); printf("\t3\tRETURN\n"); printf("\tENTER YOUR CHOICE\t:\t"); scanf("%c",&ch1); switch(ch1) { case '1': input_p(&a[0][0],r1,c1); break; case '2': input_p(&b[0][0],r2,c2); break; case '3': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch1!='3'); break; case '2': do { clrscr(); flushall(); printf("\n\n\t>---- DISPLAY MENU ----<\n"); printf("\t1\tMATRIX A\n"); printf("\t2\tMATRIX B\n"); printf("\t3\tRETURN\n"); printf("\n\tENTER YOUR CHOICE:"); scanf("%c",&ch1); switch(ch1) { case '1': if(*r1==0 || *c1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } display_p(&a[0][0],*r1,*c1); break; case '2': if(*r2==0 || *c2==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } display_p(&b[0][0],*r2,*c2); break; case '3': break; default: printf("\n\n\n\t!!! INVALID CHOICE !!! "); getch(); } }while(ch1!='3'); break; case '3': if(*r1==0 || *c1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } if(*r1==*r2 && *c1==*c2) { add_p(a,b,c,*r1,*c1); printf("ADDITION HAS BEEN PERFORMED >>>>>\n"); display_p(&c[0][0],*r1,*c1); } else { printf("\n\n\n\t!!! CAN'T ADD !!!!\n"); } getch(); break; case '4': if(*r1==*r2 && *c1==*c2) { do { clrscr(); flushall(); printf("\n\n\t>>>>---- DIFFERENCE MENU -----<<<<\n"); printf("\n\t1\tMATRIX A - MATRIX B\n"); printf("\t2\tMATRIX B - MATRIX A\n"); printf("\t3\tRETURN\n"); printf("\tENTER YOUR CHOICE:"); scanf("%c",&ch1); switch(ch1) { case '1': if(*r1==0 || *c1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } difference_p(a,b,c,*r1,*c1); printf("\n\n!!! SUBSTRACTION COMPLETE !!!\n"); display_p(&c[0][0],*r1,*c1); break; case '2': if(*r2==0 || *c2==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } difference_p(b,a,c,*r1,*c1); printf("\n\n!!! SUBSTRACTION COMPLETE !!!\n"); display_p(&c[0][0],*r1,*c1); break; case '3': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch1!='3'); }//end of if statement else printf("\n\n!!! SUBSTRACTION NOT POSSIBLE !!!"); getch(); break; case '5': do { if(*r2==0 || *c2==0 ||*c1==0 ||*r1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } clrscr(); flushall(); printf("\t\t\t\tMULTIPLICATION MENU\n"); printf("\t1\tMATRIX A * MATRIX B\n"); printf("\t2\tMATRIX B * MATRIX A\n"); printf("\t3\tRETURN\n"); printf("\n\tENTER YOUR CHOICE:"); scanf("%c",&ch1); switch(ch1) { case '1': if(*r2==*c1) { mul_p(a,b,c,*r1,*r2,*c2); printf("\n\t\t!!! MULTIPLICATION COMPLETE !!!\n"); display_p(&c[0][0],*r1,*c2); } else printf("\n\n\t!!! MULTIPLICATION NOT POSSIBLE !!!\n"); getch(); break; case '2': if(*r1==*c2) { mul_p(b,a,c,*r2,*r1,*c1); printf("\n\t\t!!! MULTIPLICATION COMPLETE !!!\n"); display_p(&c[0][0],*r2,*c1); break; } else printf("\n\n!!! MULTIPLICATION NOT POSSIBLE !!!\n"); getch(); break; case '3': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch1!='3'); break; case '6': do { clrscr(); flushall(); printf("\n\n\t>>>>---- MAGIC SQUARE MENU ----<<<<\n"); printf("\t1\tMATRIX A\n"); printf("\t2\tMATRIX B\n"); printf("\t3\tRETURN\n"); printf("\tENTER YOUR CHOICE\t"); scanf("%c",&ch1); switch(ch1) { case '1': if(*r1==0 || *c1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } if(*r1!=*c1) printf("\n\n\t!!! NOT MAGIC SQUARE !!!\n"); else { if(ms_p(a,*r1)==0) printf("\n\n\tTHE GIVEN MATRIX IS NOT A MAGIC SQUARE"); else printf("\n\n\tTHE GIVEN MATRIX IS A MAGIC SQUARE\n"); } getch(); break; case '2': if(*r2==0 || *c2==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } if(*r2 != *c2) printf("\n\t!!! THE GIVEN MATRIX IS NOT A MGIC SQUARE !!!\n"); else { if(ms_p(b,*r2)==0) printf("\n\t!!! THE GIVEN MATRIX IS NOT A MAGIC SQUARE !!!"); else printf("\n\t!!! THE GIVEN MATRIX IS A MAGIC SQUARE !!!\n"); } getch(); break; case '3': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch1!='3'); break; case '7': do { clrscr(); flushall(); printf("\n\n\t>--- SADDLE POINT MENU ---<\n"); printf("\t1\tMATRIX A\n"); printf("\t2\tMATRIX B\n"); printf("\t3\tRERURN\n"); printf("\tENTER YOUR CHOICE:"); scanf("%c",&ch1); switch(ch1) { case '1': if(*r1==0 || *c1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } m=sp_p(a,*r1,*c1); if(m==0) printf("\n\n\t !!!! NO SADDLE POINTS !!!!"); else printf("\n\n\tTOTAL %d SADDLE POINTS R PRESENT",m); getch(); break; case '2': if(*r2==0 || *c2==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } m=sp_p(b,*r2,*c2); if(m==0) printf("\n\n\t !!!! NO SADDLE POINTS !!!!"); else printf("\n\n\tTOTAL %d SADDLE POINTS R PRESENT",m); getch(); break; case '3': break; default: printf("\n\n\n\t!!! INVALID CHOICE !!! "); getch(); } }while(ch1!='3'); break; case '8': do { clrscr(); flushall(); printf("\n\n\t>---- TRANSPOSE MENU ----<\n"); printf("\t1\tMATRIX A\n"); printf("\t2\tMATRIX B\n"); printf("\t3\tEXIT\n"); printf("\n\tENTER YOUR CHOICE:"); scanf("%c",&ch1); switch(ch1) { case '1': if(*r1==0 || *c1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } if(*r1>*c1) transpose_p(a,*r1); else transpose_p(a,*c1); m=*r1; *r1=*c1; *c1=m; printf("\n\n\tMATRIX HAS BEEN TRANSPOSED\n"); display_p(&a[0][0],*r1,*c1); break; case '2': if(*r2==0 || *c2==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } if(*r2>*c2) transpose_p(b,*r2); else transpose_p(b,*c2); m=*r2; *r2=*c2; *c2=*r2; printf("\n\n\tMATRIX HAS BEEN TRANSPOSED\n"); display_p(&b[0][0],*r2,*c2); break; case '3': break; default: printf("\n\n\n\t!!! INVALID CHOICE !!! "); getch(); } }while(ch1!='3'); break; case '9': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch!='9'); } /*************************************************************** INPUT_P FUNCTION ***************************************************************/ void input_p(int*a,int *r,int *c) { int i,j; char st[max]; printf("NO. OF ROW \t"); scanf("%d",r); printf("NO. OF COLUMN \t"); scanf("%d",c); printf("\nENTER THE MATRIX \n"); for(i=0;i<*r;i++) for(j=0;j<*c;j++) { scanf("%s",st); if(valid(st)==1) *(a+ i*(max) +j)=atoi(st); else { printf("\n\t!!! ENTER AGAIN !!!!"); j--; } } } /*************************************************************** DISPLAY_P FUNCTION ***************************************************************/ void display_p(int*a,int r,int c) { int i,j; printf("\n\nTHE MATRIX IS\n"); for(i=0;i<r;i++) <br=""> { printf("\n\t"); for(j=0;j<c;j++) <br=""> { printf("%-5d",*(a+ i*max +j) ); } } getch(); } /*************************************************************** ADD_P FUNCTION ***************************************************************/ void add_p(int (*a)[max],int (*b)[max],int(*c)[max],int r,int c1) { int i,j; for(i=0;i<r;i++) <br=""> { for(j=0;j<c1;j++) <br=""> { *(c[i]+j) = *(a[i]+j) + *(b[i]+j); } } } /*************************************************************** DIFFERENCE_P FUNCTION ***************************************************************/ void difference_p(int (*a)[max],int(*b)[max],int (*c)[max],int r,int c1) { int i,j; for(i=0;i<r;i++) <br=""> { for(j=0;j<c1;j++) <br=""> *(c[i]+j)= *(a[i]+j) - *(b[i]+j); } } /*************************************************************** MUL_P FUNCTION ***************************************************************/ void mul_p(int (*a)[max],int (*b)[max],int (*c)[max],int r1,int r2,int c2) { int i,j,k; for(i=0;i<r1;++i) <br=""> { for(j=0;j<c2;++j) <br=""> { c[i][j]=0; for(k=0;k<r2;++k) <br=""> *(c[i]+j) += *(a[i]+k) * ( *(b[k]+j) ); } } } /*************************************************************** MS_P FUNCTION ***************************************************************/ int ms_p(int (*a)[max],int r) { int s1=0,i,j,s2=0,s3=0; for(i=0,j=r-1;i<r;i++,j--) <br=""> { s1+=*(a[i]+i); s2+=*(a[i]+j); } if(s1!=s2) return 0; for(i=0;i<r;i++) <br=""> { s2=0; s3=0; for(j=0;j<r;j++) <br=""> { s2+=*(a[i]+j); s3+=*(a[j]+i); } if(s1!=s2 || s3!=s1) return 0; } return 1; } /*************************************************************** SP_P FUNCTION ***************************************************************/ int sp_p(int (*a)[max],int r,int c) { int i,j,k,m=0; for(i=0;i<r;i++) <br=""> { for(j=0;j<c;j++) <br=""> { for(k=0;k<c;k++) <br=""> if( *(a[i]+j) > *(a[i]+k) ) break; if(k==c) { for(k=0;k<r;k++) <br=""> if( *(a[i]+j) < *(a[k]+j) ) break; if(k==r) { printf("\n\tSADDLE POINT AT (%d,%d) AND IS %d ",i+1,j+1,*(a[i]+j)); m++; } } }//end of 2nd for } return m; } /*************************************************************** TRANSPOSE_P FUNCTION ***************************************************************/ void transpose_p(int (*a)[max],int m) { int i,j,temp; for(i=0;i<m;i++) <br=""> { for(j=0;j<i;j++) <br=""> { temp=*(a[i]+j); *(a[i]+j)=*(a[j]+i); *(a[j]+i)=temp; } } } /******************************************************************** WITOUTP FUNCTION *********************************************************************/ void withoutp(int a[][max],int b[][max],int* r1,int* c1,int* r2,int* c2) { int c[max][max],m; char ch,ch1; do { flushall(); clrscr(); printf("\n\n\t===>>>\tWITHOUT POINTERS MENU\t<<<===\n"); printf("\n\t1\tINPUT\n"); printf("\t2\tDISPLAY\n"); printf("\t3\tADDITION\n"); printf("\t4\tDIFFERENCE\n"); printf("\t5\tMULTIPLICATION\n"); printf("\t6\tMAGIC SQUARE\n"); printf("\t7\tSADDLE POINT\n"); printf("\t8\tTRANSPOSE\n"); printf("\t9\tRETURN TO MAIN MENU\n"); printf("\n\tENTER YOUR CHOICE\t:\t"); scanf("%c",&ch); switch(ch) { case '1': do { flushall(); clrscr(); printf("\n\t>>>>---- INPUT MENU ----<<<<\n"); printf("\t1\tMATRIX A\n"); printf("\t2\tMATRIX B\n"); printf("\t3\tRETURN\n"); printf("\tENTER YOUR CHOICE\t:\t"); scanf("%c",&ch1); switch(ch1) { case '1': input(a,r1,c1); break; case '2': input(b,r2,c2); break; case '3': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch1!='3'); break; case '2': do { clrscr(); flushall(); printf("\n\n\t>---- DISPLAY MENU ----<\n"); printf("\t1\tMATRIX A\n"); printf("\t2\tMATRIX B\n"); printf("\t3\tRETURN\n"); printf("\n\tENTER YOUR CHOICE:"); scanf("%c",&ch1); switch(ch1) { case '1': display(a,*r1,*c1); break; case '2': display(b,*r2,*c2); break; case '3': break; default: printf("\n\n\n\t!!! INVALID CHOICE !!! "); getch(); } }while(ch1!='3'); break; case '3': if(*r1==*r2 && *c1==*c2) { add(a,b,c,*r1,*c1); printf("ADDITION HAS BEEN PERFORMED >>>>>\n"); display(c,*r1,*c1); } else { printf("\n\n\n\t!!! CAN'T ADD !!!!\n"); } getch(); break; case '4': if(*r1==*r2 && *c1==*c2) { do { clrscr(); flushall(); printf("\n\n\t>>>>---- DIFFERENCE MENU -----<<<<\n"); printf("\n\t1\tMATRIX A - MATRIX B\n"); printf("\t2\tMATRIX B - MATRIX A\n"); printf("\t3\tRETURN\n"); printf("\tENTER YOUR CHOICE:"); scanf("%c",&ch1); switch(ch1) { case '1': difference(a,b,c,*r1,*c1); printf("\n\n!!! SUBSTRACTION COMPLETE !!!\n"); display(c,*r1,*c1); break; case '2': difference(b,a,c,*r1,*c1); printf("\n\n!!! SUBSTRACTION COMPLETE !!!\n"); display(c,*r1,*c1); break; case '3': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch1!='3'); }//end of if statement else printf("\n\n!!! SUBSTRACTION NOT POSSIBLE !!!"); getch(); break; case '5': do { if(*r2==0 || *c2==0 ||*c1==0 ||r1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } clrscr(); flushall(); printf("\t\t\t\tMULTIPLICATION MENU\n"); printf("\t1\tMATRIX A * MATRIX B\n"); printf("\t2\tMATRIX B * MATRIX A\n"); printf("\t3\tRETURN\n"); printf("\n\tENTER YOUR CHOICE:"); scanf("%c",&ch1); switch(ch1) { case '1': if(*r2==*c1) { mul(a,b,c,*r1,*r2,*c2); printf("\n\t\t!!! MULTIPLICATION COMPLETE !!!\n"); display(c,*r1,*c2); } else printf("\n\n\t!!! MULTIPLICATION NOT POSSIBLE !!!\n"); getch(); break; case '2': if(*r1==*c2) { mul(b,a,c,*r2,*r1,*c1); printf("\n\t\t!!! MULTIPLICATION COMPLETE !!!\n"); display(c,*r2,*c1); break; } else printf("\n\n!!! MULTIPLICATION NOT POSSIBLE !!!\n"); getch(); break; case '3': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch1!='3'); break; case '6': do { clrscr(); flushall(); printf("\n\n\t>>>>---- MAGIC SQUARE MENU ----<<<<\n"); printf("\t1\tMATRIX A\n"); printf("\t2\tMATRIX B\n"); printf("\t3\tRETURN\n"); printf("\tENTER YOUR CHOICE\t"); scanf("%c",&ch1); switch(ch1) { case '1': if(*r1==0 || *c1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } if(*r1!=*c1) printf("\n\n\t!!! NOT MAGIC SQUARE !!!\n"); else { if(ms(a,*r1)==0) printf("\n\n\tTHE GIVEN MATRIX IS NOT A MAGIC SQUARE"); else printf("\n\n\tTHE GIVEN MATRIX IS A MAGIC SQUARE\n"); } getch(); break; case '2': if(*r2==0 || *c2==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } if(*r2 != *c2) printf("\n\t!!! THE GIVEN MATRIX IS NOT A MGIC SQUARE !!!\n"); else { if(ms(b,*r2)==0) printf("\n\t!!! THE GIVEN MATRIX IS NOT A MAGIC SQUARE !!!"); else printf("\n\t!!! THE GIVEN MATRIX IS A MAGIC SQUARE !!!\n"); } getch(); break; case '3': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch1!='3'); break; case '7': do { clrscr(); flushall(); printf("\n\n\t>--- SADDLE POINT MENU ---<\n"); printf("\t1\tMATRIX A\n"); printf("\t2\tMATRIX B\n"); printf("\t3\tRERURN\n"); printf("\tENTER YOUR CHOICE:"); scanf("%c",&ch1); switch(ch1) { case '1': if(*r1==0 || *c1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } m=sp(a,*r1,*c1); if(m==0) printf("\n\n\t !!!! NO SADDLE POINTS !!!!"); else printf("\n\n\tTOTAL %d SADDLE POINTS R PRESENT",m); getch(); break; case '2': if(*r2==0 || *c2==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } m=sp(b,*r2,*c2); if(m==0) printf("\n\n\t !!!! NO SADDLE POINTS !!!!"); else printf("\n\n\tTOTAL %d SADDLE POINTS R PRESENT",m); getch(); break; case '3': break; default: printf("\n\n\n\t!!! INVALID CHOICE !!! "); getch(); } }while(ch1!='3'); break; case '8': do { clrscr(); flushall(); printf("\n\n>---- TRANSPOSE MENU ----<\n"); printf("\t1\tMATRIX A\n"); printf("\t2\tMATRIX B\n"); printf("\t3\tEXIT\n"); printf("\n\tENTER YOUR CHOICE:"); scanf("%c",&ch1); switch(ch1) { case '1': if(*r1==0 || *c1==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } if(*r1>*c1) transpose(a,*r1); else transpose(a,*c1); m=*r1; *r1=*c1; *c1=m; printf("\n\n\tMATRIX HAS BEEN TRANSPOSED\n"); display(a,*r1,*c1); break; case '2': if(*r2==0 || *c2==0) { printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!"); getch(); break; } if(*r2>*c2) transpose(a,*r2); else transpose(a,*c2); m=*r2; *r2=*c2; *c2=*r2; printf("\n\n\tMATRIX HAS BEEN TRANSPOSED\n"); display(a,*r2,*c2); break; case '3': break; default: printf("\n\n\n\t!!! INVALID CHOICE !!! "); getch(); } }while(ch1!='3'); break; case '9': break; default: printf("\n\n\t!!! INVALID CHOICE !!!"); getch(); } }while(ch!='9'); } /*************************************************************** INPUT FUNCTION ***************************************************************/ void input(int a[][max],int *r,int *c) { int i,j; char st[max]; printf("NO. OF ROW \t"); scanf("%d",r); printf("NO. OF COLUMN \t"); scanf("%d",c); printf("\nENTER THE MATRIX \n"); for(i=0;i<*r;i++) for(j=0;j<*c;j++) { scanf("%s",st); if(valid(st)==1) a[i][j]=atoi(st); else { printf("\n\t!!! ENTER AGAIN !!!!"); j--; } } } /*************************************************************** VALID FUNCTION ***************************************************************/ int valid(char st[]) { int i=0; for(;st[i]!=NULL;i++) { if(st[i]<48 ||st[i]>58) return 0; } return 1; } /*************************************************************** DISPLAY FUNCTION ***************************************************************/ void display(int a[][max],int r,int c) { int i,j; printf("\n\nTHE MATRIX IS\n"); for(i=0;i<r;i++) <br=""> { printf("\n\t"); for(j=0;j<c;j++) <br=""> { printf("%-5d",a[i][j]); } } getch(); } /*************************************************************** ADD FUNCTION ***************************************************************/ void add(int a[][max],int b[][max],int c[][max],int r,int c1) { int i,j; for(i=0;i<r;i++) <br=""> { for(j=0;j<c1;j++) <br=""> { c[i][j]=a[i][j] + b[i][j]; } } } /*************************************************************** DIFFERENCE FUNCTION ***************************************************************/ void difference(int a[][max],int b[][max],int c[][max],int r,int c1) { int i,j; for(i=0;i<r;i++) <br=""> { for(j=0;j<c1;j++) <br=""> { c[i][j]=a[i][j] - b[i][j]; } } } /*************************************************************** MUL FUNCTION ***************************************************************/ void mul(int a[][max],int b[][max],int c[][max],int r1,int r2,int c2) { int i,j,k; for(i=0;i<r1;++i) <br=""> { for(j=0;j<c2;++j) <br=""> { c[i][j]=0; for(k=0;k<r2;++k) <br=""> c[i][j]+=a[i][k] * b[k][j]; } } } /*************************************************************** MS FUNCTION ***************************************************************/ int ms(int a[][max],int r) { int s1=0,i,j,s2=0,s3=0; for(i=0,j=r-1;i<r;i++,j--) <br=""> { s1+=a[i][i]; s2+=a[i][j]; } if(s1!=s2) return 0; for(i=0;i<r;i++) <br=""> { s2=0; s3=0; for(j=0;j<r;j++) <br=""> { s2+=a[i][j]; s3+=a[j][i]; } if(s1!=s2 || s3!=s1) return 0; } return 1; } /*************************************************************** SP FUNCTION ***************************************************************/ int sp(int a[][max],int r,int c) { int i,j,k,m=0; for(i=0;i<r;i++) <br=""> { for(j=0;j<c;j++) <br=""> { for(k=0;k<c;k++) <br=""> if(a[i][j] > a[i][k]) break; if(k==c) { for(k=0;k<r;k++) <br=""> if(a[i][j] < a[k][j]) break; if(k==r) { printf("\n\tSADDLE POINT AT (%d,%d) AND IS %d ",i+1,j+1,a[i][j]); m++; } } }//end of 2nd for } return m; } /*************************************************************** TRANSPOSE FUNCTION ***************************************************************/ void transpose(int a[][max],int m) { int i,j,temp; for(i=0;i<m;i++) <br=""> { for(j=0;j<i;j++) <br=""> { temp=a[i][j]; a[i][j]=a[j][i]; a[j][i]=temp; } } }
Leave a Reply