Showing posts with label addition. Show all posts
Showing posts with label addition. Show all posts

Saturday, August 9, 2014

Program for polynomial addition

This is a program for polynomial addition. Please be sure to enter the coefficients in the decreasing order of power. Comment below if you need help
#include<stdio.h> struct node { int coef; int pow; struct node *link; }*poly,*poly1=NULL,*poly2=NULL,*node,*next; void polcreate(struct node *next) { int ch; do { printf("\nCoeff of the term?"); scanf("%d",&next->coef); printf("\nPower of the term?"); scanf("%d",&next->pow); next->link=(struct node*)malloc(sizeof(struct node)); next=next->link; printf("\nEnter 1 to continue press 2 to terminate the expression : "); scanf("%d",&ch); }while(ch==1); } void show(struct node *next) { while(next->link!=NULL) { printf("%dX^%d",next->coef,next->pow); next= next->link; if( next->link!=NULL) printf("+"); } printf("\n"); } void polyadd(struct node *poly1,struct node *poly2,struct node *poly) { while(poly1->link && poly2->link) { if(poly1->pow > poly2->pow) { poly->pow= poly1->pow; poly->coef= poly1->coef; poly1= poly1->link; } else if(poly1->pow < poly2->pow) { poly->pow= poly2->pow; poly->coef= poly2->coef; poly2= poly2->link; } else { poly->pow= poly1->pow; poly->coef= poly1->coef + poly2->coef; poly1= poly1->link; poly2= poly2->link; } poly->link= (struct node*)malloc(sizeof(struct node)); poly=poly->link; poly->link=NULL; } while(poly1->link||poly2->link) { if(poly1->link) { poly->pow=poly1->pow; poly->coef=poly1->coef; poly1= poly1->link; } if(poly2->link) { poly->pow= poly2->pow; poly->coef= poly2->coef; poly2= poly2->link; } poly->link= (struct node*)malloc(sizeof(struct node)); poly = poly->link; poly->link=NULL; } } int main() { poly1=(struct node*)malloc(sizeof(struct node)); poly2=(struct node*)malloc(sizeof(struct node)); poly=(struct node*)malloc(sizeof(struct node)); printf("\nPOLYNOMIAL 1(decreasing order of power)\n"); polcreate(poly1); printf("\nPOLYNOMIAL 2(decreasing order of power)\n"); polcreate(poly2); printf("\nFirst polynomial : \n"); show(poly1); printf("Second polynomial : \n"); show(poly2); polyadd(poly1,poly2,poly); printf("\n sum is as follows\n"); show(poly); }

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]); } }