-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomialAdd.cpp
More file actions
45 lines (41 loc) · 1.23 KB
/
polynomialAdd.cpp
File metadata and controls
45 lines (41 loc) · 1.23 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
#include <iostream>
int main()
{
int firstArrDegree = 0;
int secondArrDegree = 0;
int highDeg = 0;
std::cout << "Please enter the highest degree of the first polynomial" << std::endl;
std::cin >> firstArrDegree;
std::cout << "Please Enter the highest degree of the second polynomial" << std::endl;
std::cin >> secondArrDegree;
int *firstPolynomial = new int[firstArrDegree];
int *secondPolynomial = new int[secondArrDegree];
if(firstArrDegree > secondArrDegree)
highDeg = firstArrDegree;
else
highDeg = secondArrDegree;
int *polySum = new int[highDeg]();
std::cout << "Please enter the coefficients of terms of the first polynomial" << std::endl;
for(int i = 0; i < firstArrDegree; i++)
{
std::cin >> *(firstPolynomial + i);
}
std::cout << "Please enter the coefficients of terms of the second polynomial" << std::endl;
for(int j = 0; j < secondArrDegree; j++)
{
std::cin >> *(secondPolynomial + j);
}
for(int k = 0; k <= highDeg; k++)
{
polySum[k] = firstPolynomial[k] + secondPolynomial[k];
}
for(int k = highDeg; k >= 0; k--)
{
if(*(polySum + k) != 0)
std::cout << *(polySum + k) <<"x^" << k << "+";
}
std::cout << "\n";
delete [] polySum;
delete [] secondPolynomial;
delete [] firstPolynomial;
}