Unit #5 Homework Problem (Solution)
Course Outline HW & Programming Assignments
Sol5hw2a

Write a function void poly ( float x, float& y) which returns the value of the polynomial:

y = x3 + 4x2 +5.6x + a

 

for a given x. Here a = 7 for positive x, a = - 3 for negative x, and a = 0 for zero x.


ANSWER:

#include <iostream.h>
#include <math.h>
void poly ( float x,          float& y);
main()
{
float x = 2, y;
poly( x, y);
cout <<"\n\n\n          x = " << x << ", y = " << y;
char hold;
cout << "          \n\n enter e to exit " ;
cin >> hold;
}
void poly ( float x,          float& y)
{
float a ;
if ( x > 0.0 ) a          = 7;
 else if ( x == 0.0 )          a = 0.0;
 else a = -3.0;
 y = pow(x, 3) + 4 *          x * x + 5.6 * x + a;
}
/* A TYPICAL SESSION
x = 2, y = 42.2
enter e to exit
*/ 
Jacob Y. Kazakia © 2001 All rights reserved