Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. // CSIT 121
  2. // Dr. Straight
  3. // Problem: Choose a poynomial p(x) with nonnegative-integer coefficients and at least two terms.
  4. // Input p(1) and p(p(1)).
  5. // Output the degree of p(x) and its coefficients.
  6.  
  7. // Program written by (Your Name)
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <cmath>
  12. using namespace std;
  13.  
  14. int degree(int z2,int z1)
  15. //Computes the degree of p(x).
  16. {
  17. return floor(log(z2)/log(z1));
  18. }
  19.  
  20. void coefficients(int n,int b)
  21. //Computes and outputs the coefficients of p(x).
  22. //Essentially, this computes the base-b representation of n.
  23. {
  24. int i, d, q = n, r;
  25. d = degree(n,b);
  26. for (i = 0; i <= d; i = i + 1)
  27. {
  28. r = q % b;
  29. cout << setw(10) << r << endl;
  30. q = (q - r)/b;
  31. }
  32. }
  33.  
  34. int main()
  35. {
  36. //Variables used:
  37. int y1; //y1 = p(1)
  38. int y2; //y2 = p(p(1))
  39. int d; //degree of p(x)
  40.  
  41. //Prompt the user for p(1) and p(p(1)).
  42. cout << "Choose a polynomial p(x) with nonnegative integer coefficients and" << endl;
  43. cout << "at least two terms." << endl;
  44. cout << "Enter p(1) (enter 0 or a negative integer to quit): ";
  45. cin >> y1;
  46. While (y1 > 0)
  47. {
  48. cout << "Enter p(p(1)): ";
  49. cin >> y2;
  50. cout << "The Great Mathemagician will now compute your polynomial." << endl;
  51. d = degree(y2,y1);
  52. cout << "The degree of your polynomial is " << d << endl;
  53. cout << "The coefficients of your polynomial, in order of increasing" << endl;
  54. cout << "term-power, are as follows:" << endl;
  55. coefficients(y2,y1);
  56. cout << endl;
  57. cout << "Choose another polynomial p(x)." << endl;
  58. cout << "Enter p(1) (0 or a negative integer to quit): ";
  59. cin >> y1;
  60. }
  61. system("pause");
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement