A Polynomial function is a function of the form: P(x) = 0,*" + an-14 *an-zxn-1 + ax + ao where an, On-1,...,01, ao are real numbers and n is non-negative. Write a C++ program that reads two polynomials and output their sum, i.e. if P1(x) = 3x4 + 5x3 + x P2(x) = 2x5 - 5x4 - x + 2 their sum is: P1(x) + P2(x) = 2x5 - 2x4 + 5x3 + 2 NOTE: Polynomials should be entered as [ 3x4+5x3+x] where 3,5 and 1 are the coefficients of x4, x3 and xl, respectively. Your program should give an error message if a non valid polynomial is entered.
To store your polynomial you can use a one-dimensional array: the coefficient in the array; the power is the index (position) of array. Also note that all powers are bigger than 0. Thus 5x-2 is 5x minus 2; power cannot be negative.
#include <iostream> using namespace std; int main() { double polynomial[10] - 0); char c; double coefficient = 0; int power = 0; bool next Power = false; while (c = cin.get(), c != '\n') { if (c == '+' || C == '') nextPower = false; else if (c == 'X' Il c == 'X') nextPower - true; else if (c >= '@' && C <= '9') { cin.unget(); if (nextPower) { cin >> power; polynomial[power] = coefficient; nextPower = false; } else cin >> coefficient; } } for (int i = 9; i >= 0; i--) if (polynomial != 0) { cout << "coefficient = " << polynomial << " power = " << i << endl; } return; }
A Polynomial function is a function of the form: P(x) = 0,*" + an-14 *an-zxn-1 + ax + ao where an, On-1,...,01, ao are r
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
A Polynomial function is a function of the form: P(x) = 0,*" + an-14 *an-zxn-1 + ax + ao where an, On-1,...,01, ao are r
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!