0

Add two polynomial a [ ], b[ ] in a polynomial c[ ] can any one answer

10th Nov 2016, 5:53 PM
Shahida M Sakr
Shahida M Sakr - avatar
5 Answers
+ 1
not sure if you mean to get polynomial c as the sum of polynomials a and b. if so, very easy if you store the coefficients of the polynomial in an array. just add the corresponding elements of the two array.
18th Nov 2016, 10:02 PM
Rill Chritty
Rill Chritty - avatar
+ 1
unfortunately, it does not allow to post the full code (too large, I suppose). so, I will post the code in parts. Part 1: /*This is just a crude implementation of the solution to give an idea of how to proceed. There are definitely better ways to do this, but this works just fine*/ #include<iostream> #include<cstdlib> #include<string> #include<vector> using namespace std; vector<double> split(string str, char delimiter = ' ') { size_t start = 0, end = 0; string temp; vector<double> result; while(end != string::npos){ start = str.find_first_not_of(string(1, delimiter), start); end = str.find(delimiter, start); if(start != end){ temp = str.substr(start, (end == string::npos) ? str.size() : end); start = end + 1; //double num = stod(temp); //you need c++11 and above to use the function stod double num = strtod(temp.c_str(), NULL); //if your compiler does not support c++11, then use strtod, you have to include the header file "cstdlib" result.push_back(num); } } return result; } void printPolynomial(vector<double> polynom) { for(int i = polynom.size() - 1; i > 1; i--){ if(polynom[i]){ //we won't print terms with zero coefficients cout << showpos << polynom[i]; cout << noshowpos << "x^" << i; } } if(polynom[1]){ cout << showpos << polynom[1] << "x"; } if(polynom[0]){ cout << polynom[0]; } cout << endl; }
19th Nov 2016, 12:22 PM
Rill Chritty
Rill Chritty - avatar
+ 1
Part 2: int main() { cout << "The polynomials should be entered in the format (a0 a b c d ...) where a0, a, b, c,... are the coefficients of the monomials x0, x, x^2, x^3... (reverse order for easy computation). For example, the polynomial -7x^5 + 8x^3 - 0.5x^2 + 9 should be entered as 9 0 -0.5 8 0 -7" << endl; cout << "Enter polynomial A: " << endl; string input1, input2; getline(cin, input1); cout << "Enter polynomial B: " << endl; getline(cin, input2); char delimiter = ' '; //The character separating coefficients, you can change it to whatever you want vector<double> polynom1 = split(input1, delimiter); vector<double> polynom2 = split(input2, delimiter); vector<double> polynom3; int size1 = polynom1.size(); int size2 = polynom2.size(); int maxSize = (size1 > size2) ? size1 : size2; polynom3.resize(maxSize); //set the size of polynomial C //pad the smaller polynomial with zeros if(size1 < maxSize){ polynom1.resize(maxSize); } else if (size2 < maxSize){ polynom2.resize(maxSize); } for(int i = 0; i < maxSize; i++){ polynom3[i] = polynom1[i] + polynom2[i]; } cout << "Polynomial A: "; printPolynomial(polynom1); cout << "Polynomial B: "; printPolynomial(polynom2); cout << "Polynomial C = A + B:\n"; printPolynomial(polynom3); return 0; }
19th Nov 2016, 12:23 PM
Rill Chritty
Rill Chritty - avatar
0
could you write the code is c++, please?
19th Nov 2016, 4:37 AM
Shahida M Sakr
Shahida M Sakr - avatar
0
thank you so much 😊
19th Nov 2016, 3:37 PM
Shahida M Sakr
Shahida M Sakr - avatar