fork download
  1. // C++ program to implement vieta formula
  2. // to calculate polynomial coefficients.
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. // Function to calculate polynomial
  7. // coefficients.
  8. void vietaFormula(int roots[], int n)
  9. {
  10. // Declare an array for
  11. // polynomial coefficient.
  12. int coeff[n + 1];
  13.  
  14. // Set all coefficients as zero initially
  15. memset(coeff, 0, sizeof(coeff));
  16.  
  17. // Set highest order coefficient as 1
  18. coeff[0] = 1;
  19.  
  20. for (int i = 0; i < n; i++) {
  21. cout<<i<<":"<<endl;
  22. for (int j = i+1; j >0; j--) {
  23.  
  24. coeff[j] += roots[i] * coeff[j - 1];
  25. cout<<j<<" "<<coeff[j]<<" "<<roots[i]<<" "<<coeff[j-1]<<endl;
  26. }
  27. }
  28.  
  29. cout << "Polynomial Coefficients: ";
  30. for (int i = n; i >= 0; i--) {
  31. cout << coeff[i] << " ";
  32. }
  33. }
  34.  
  35. // Driver code
  36. int main()
  37. {
  38. // Degree of required polynomial
  39. int n = 4;
  40.  
  41. // Initialise an array by
  42. // root of polynomial
  43. int roots[] = {-1, 2, -3, 7};
  44.  
  45. // Function call
  46. vietaFormula(roots, n);
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5536KB
stdin
Standard input is empty
stdout
0:
1 -1 -1 1
1:
2 -2 2 -1
1 1 2 1
2:
3 6 -3 -2
2 -5 -3 1
1 -2 -3 1
3:
4 42 7 6
3 -29 7 -5
2 -19 7 -2
1 5 7 1
Polynomial Coefficients: 42 -29 -19 5 1