// polynomial1.cpp
// Computes a polynomial.
// Demonstrates unnecessary nested loops.
#include <iostream>
int main()
{
cout << "This program computes the value "
<< "of a polynomial with" << endl;
cout << "coefficients and variable value "
<< "entered by the user." << endl;
cout << "Enter the value of the variable x:>";
double x;
cin >> x;
cout << "Enter the degree of the polynomial:>";
int degree;
cin >> degree;
if ( degree < 0 )
{
cout << "Degree " << degree
<< " must be non-negative." << endl;
return 1;
} // if
// Cumulative sum of terms of polynomial:
double sum = 0;
// Ask user for coefficients, compute terms,
// and add to cumunlative sum:
for ( int order = degree; order >= 1; order-- )
{
// Ask user for the coefficient:
cout << "Enter the value of the order-"
<< order << " coefficient:>";
double coefficient;
cin >> coefficient;
// Compute the term's power of x:
double powerOfX = 1;
for ( int i = 0; i < order; i++ )
powerOfX = powerOfX * x;
// Add term to cumulative sum:
sum = sum + coefficient * powerOfX;
} // for order
// Ask user for the constant term and add it to sum:
cout << "Enter the value of the "
<< "constant term:>";
double constantTerm;
cin >> constantTerm;
sum = sum + constantTerm;
// Output result:
cout << "The value of the polynomial is "
<< sum << "." << endl;
return 0;
} // function main