Showing posts with label matrices. Show all posts
Showing posts with label matrices. Show all posts

Saturday, August 9, 2014

Addition, multiplication and transpose of matrices

This is a program for the addition, multiplication and transpose of matrices. If you have any doubts, please let me know.

#include<stdio.h> main() { int m,n,i,j,k,p,q,r,a[25][25],b[25][25],c[25][25],ch; printf("\n Enter the rows and columns"); scanf("%d %d",&m,&n); printf("\n Enter the value of the array"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } printf("\n MENU \n 1)addition 2)multiplication 3)transpose \n Enter your choice"); scanf("%d",&ch); if(ch==1) { printf("\n Enter the value of the array b"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); c[i][j]=a[i][j]+b[i][j]; } } printf("\n the array is \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" %d ",c[i][j]); } printf("\n"); } } if(ch==2) { printf("\n Enter the value of array b"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][i]; } } } printf("\n the new array is \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" %d ",c[i][j]); } printf("\n"); } } if(ch==3) { for(i=0;i<m;i++) { for(j=0;j<n;j++) b[i][j]=a[j][i]; } printf("\n the new array is \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" %d ",b[i][j]); } printf("\n"); } } }

SImple Matrix addition

Simple C program for the addition of 2 matrices. If you have any doubts, please let me know.

#include<stdio.h> #define size 30 int sp1[size][3],sp2[size][3],sp3[size][3],s1=0,s2=0,s3=0,r,c; int main() { void add(); int a[size][size],i,j; printf("\n\n\t\tEnter number of rows and columns :"); scanf("%d%d",&r,&c); printf("\n\n\t\tEnter matrix 1 : \n"); for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d",&a[i][j]); for(i=0;i<r;i++) for(j=0;j<c;j++) { if(a[i][j]!=0) { s1++; sp1[s1][0]=i; sp1[s1][1]=j; sp1[s1][2]=a[i][j]; } } printf("\n\n\t\tEnter matrix 2 : \n"); for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d",&a[i][j]); for(i=0;i<r;i++) for(j=0;j<c;j++) { if(a[i][j]!=0) { s2++; sp2[s2][0]=i; sp2[s2][1]=j; sp2[s2][2]=a[i][j]; } } sp1[0][0]=r; sp1[0][1]=c; sp1[0][2]=s1; sp2[0][0]=r; sp2[0][1]=c; sp2[0][2]=s2; printf("\n\n\t\t The matrices are\n\t\t\tA : \n"); for(i=0;i<=s1;i++) { printf("\n"); for(j=0;j<3;j++) printf("\t %d",sp1[i][j]); } printf("\n\t\t\tB : \n"); for(i=0;i<=s2;i++) { printf("\n"); for(j=0;j<3;j++) printf("\t %d",sp2[i][j]); } printf("\n\n"); add(); printf("\n\n"); } void add() { int i=1,j=1; while((i!=s1+1)&&(j!=s2+1)) { if(sp1[i][0]==sp2[j][0]) { if(sp1[i][1]==sp2[j][1]) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]+sp2[j][2]; i++; j++; } else if(sp1[i][1]<=sp2[j][1]) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]; j++; } else { s3++; sp3[s3][0]=sp2[j][0]; sp3[s3][1]=sp2[j][1]; sp3[s3][2]=sp2[j][2]; j++; } } else if(sp1[i][0]<sp2[j][0]) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]; i++; } else { s3++; sp3[s3][0]=sp2[j][0]; sp3[s3][1]=sp2[j][1]; sp3[s3][2]=sp2[j][2]; j++; } } if(i!=s1+1) { while(i!=s1+1) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]; i++; } } if(j!=s2+1) { while(j!=s2+1) { s3++; sp3[s3][0]=sp2[j][0]; sp3[s3][1]=sp2[j][1]; sp3[s3][2]=sp2[j][2]; j++; } } sp3[0][0]=r; sp3[0][1]=c; sp3[0][2]=s3; printf("\nThe Sum is :\n"); for(i=0;i<=s3;i++) { printf("\n"); for(j=0;j<3;j++) printf("\t%d",sp3[i][j]); } }