Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CSIT 121
- // Dr. Straight
- // Problem: Choose a poynomial p(x) with nonnegative-integer coefficients and at least two terms.
- // Input p(1) and p(p(1)).
- // Output the degree of p(x) and its coefficients.
- // Program written by (Your Name)
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- int degree(int z2,int z1)
- //Computes the degree of p(x).
- {
- return floor(log(z2)/log(z1));
- }
- void coefficients(int n,int b)
- //Computes and outputs the coefficients of p(x).
- //Essentially, this computes the base-b representation of n.
- {
- int i, d, q = n, r;
- d = degree(n,b);
- for (i = 0; i <= d; i = i + 1)
- {
- r = q % b;
- cout << setw(10) << r << endl;
- q = (q - r)/b;
- }
- }
- int main()
- {
- //Variables used:
- int y1; //y1 = p(1)
- int y2; //y2 = p(p(1))
- int d; //degree of p(x)
- //Prompt the user for p(1) and p(p(1)).
- cout << "Choose a polynomial p(x) with nonnegative integer coefficients and" << endl;
- cout << "at least two terms." << endl;
- cout << "Enter p(1) (enter 0 or a negative integer to quit): ";
- cin >> y1;
- While (y1 > 0)
- {
- cout << "Enter p(p(1)): ";
- cin >> y2;
- cout << "The Great Mathemagician will now compute your polynomial." << endl;
- d = degree(y2,y1);
- cout << "The degree of your polynomial is " << d << endl;
- cout << "The coefficients of your polynomial, in order of increasing" << endl;
- cout << "term-power, are as follows:" << endl;
- coefficients(y2,y1);
- cout << endl;
- cout << "Choose another polynomial p(x)." << endl;
- cout << "Enter p(1) (0 or a negative integer to quit): ";
- cin >> y1;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement