forked from nitinsultania/CPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyoperations.c
111 lines (102 loc) · 2.09 KB
/
polyoperations.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// LANGUAGE: c
// ENV: gcc
// AUTHOR: Nitin Sultania
// GITHUB: https://github.com/nitinsultania
#include<stdio.h>
#include<stdlib.h>
int *sum(int *A,int *B,int m,int n)
{
int *C;
if(m>n)
{
C=(int *)malloc((m+1)*sizeof(int));
for(int i=0;i<=m;i++)
{
C[i]=A[i];
}
for(int i=0;i<=n;i++)
{
C[i]+=B[i];
}
}
else
{
C=(int *)malloc((n+1)*sizeof(int));
for(int i=0;i<=n;i++)
{
C[i]=B[i];
}
for(int i=0;i<=m;i++)
{
C[i]+=A[i];
}
}
return C;
}
int *multiply(int *A,int *B,int m,int n)
{
int *C;
C=(int *)malloc((m+n+1)*sizeof(int));
for(int i=0;i<=m+n+1;i++)
{
C[i]=0;
}
for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;j++)
{
C[i+j]+=(A[i]*B[j]);
}
}
return C;
}
int main()
{
int m,n,k,coff,exp;
printf("Enter the max power in poly 1: ");
scanf("%d",&m);
printf("Enter the max power in poly 2: ");
scanf("%d",&n);
int *A=(int *)malloc((m+1)*sizeof(int));
for(int i=0;i<=m;i++)
{
A[i]=0;
}
printf("Enter number of terms in poly 1: ");
scanf("%d",&k);
for(int i=0;i<k;i++)
{
printf("Enter the coff and power");
scanf("%d%d",&coff,&exp);
A[exp]=coff;
}
int *B=(int *)malloc((n+1)*sizeof(int));
for(int i=0;i<=n;i++)
{
B[i]=0;
}
printf("Enter number of terms in poly 2: ");
scanf("%d",&k);
for(int i=0;i<k;i++)
{
printf("Enter the coff and power");
scanf("%d%d",&coff,&exp);
B[exp]=coff;
}
int *C=sum(A,B,m,n);
int size=(m>n?m:n);
for(int i=0;i<=size;i++)
{
if(C[i]==0)
continue;
printf("%d^%d ",C[i],i);
}
int *D=multiply(A,B,m,n);
for(int i=0;i<=m+n+1;i++)
{
if(D[i]==0)
continue;
printf("%d^%d ",D[i],i);
}
return 0;
}