-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdditionsparseMatrix.c
More file actions
153 lines (138 loc) · 3.12 KB
/
AdditionsparseMatrix.c
File metadata and controls
153 lines (138 loc) · 3.12 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void print_poly(int d[], int size)
{
for (int i=size; i>= 0; i--)
{
if (d[i] < 0)
{
printf("\b\b");
printf("- %dx^(%d) + ", (-1 * d[i]), i);
}
else
printf("%dx^(%d) + ", d[i],i);
}
printf("\b");
printf("\b \n");
}
void multiplicaton(int a[], int b[], int c[], int m, int n)
{
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
c[i + j] += a[i] * b[j];
}
}
printf("Multiplication Result : ");
print_poly(c, m + n);
}
void subtraction(int a[], int b[], int m, int n)
{
printf("Subtraction Result : ");
int min = MIN(m, n);
if (min == n)
{
int *c = calloc((m), sizeof(int));
for (int i = 0; i <= m; i++)
{
c[i] = a[i] ;
}
for (int i = 0; i <= min; i++)
{
c[i] -= b[i] ;
}
print_poly(c, m);
}
else
{
int *c = calloc((n), sizeof(int));
for (int i = 0; i <= n; i++)
{
c[i] = b[i] ;
}
for (int i = 0; i <= min; i++)
{
b[i] -= a[i];
}
print_poly(b, n);
}
}
void addition(int a[], int b[], int m, int n)
{
printf("Addition Result : ");
int min = MIN(m, n);
if (min == n)
{
int *c = calloc((m),sizeof(int));
for (int i = 0; i <= m; i++)
{
c[i] = a[i] ;
}
for (int i = 0; i <= min; i++)
{
c[i] += b[i];
}
print_poly(c, m);
}
else
{
int *c = calloc((n), sizeof(int));
for (int i = 0; i <= n; i++)
{
c[i] = b[i] ;
}
for (int i = 0; i <= min; i++)
{
c[i] += a[i];
}
print_poly(c, n);
}
}
int main()
{
int m, n;
printf("Enter the degree of both the polynomials m n : ");
scanf("%d%d", &m, &n);
int *a = calloc(m, sizeof(int));
int *b = calloc(n, sizeof(int));
int *c = calloc((m + n), sizeof(int));
printf("\nEnter the coefficients of polynomial with degree m : ");
for (int i = 0; i <= m; i++)
{
scanf("%d", a + i);
}
printf("\nEnter the coefficients of polynomial with degree n : ");
for (int i = 0; i <= n; i++)
{
scanf("%d", b + i);
}
printf("\nPolynomial 1 : ");
print_poly(a, m);
printf("Polynomial 2 : ");
print_poly(b, n);
printf("\n");
int k = 3 , choice;
while (k --> 0)
{
printf("Enter the choice of operation : \n1. Addition \n2. Subtraction \n3. Multiplicaton \n\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
addition(a, b, m, n);
break;
case 2:
subtraction(a, b, m, n);
break;
case 3:
multiplicaton(a, b, c, m, n);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}