I've written a program that finds the max of a function. I now want to get rid of the equation y=x^2-7x-18 and replace it by y=fx, fx being an equation that the user inputs. However, I'm not sure of how to go about doing so. What is the best data type to declare the user-input equation as? And will I need to do anything special with the user-input equation inside of the for loop, or can I just replacen y = pow(x, 2)-7*x-18 with y = fx?
Here's my current code
#include <iostream>
#include <cmath>
#include <cstdlib>
usingnamespace std;
int main()
{
int a, b, delta, x, y;
double max= -1.8 * pow(10, 308);
cout <<"Please enter the first number of the interval to be checked: " << endl;
cin >> a;
cout << "Please enter the last number of the interval to be checked: " << endl;
cin >> b;
delta= 1;
for(x = a; x <= b; x = x+delta)
{
y = pow(x, 2)-7*x-18;
if (y > max)
{
max = y;
}
else
{
delta= delta/2;
}
if (delta < pow( 10, -6))
{
break;
}
}
cout <<"The maximum over the interval from " << a <<" to " << b <<" is " << max;
return 0;
}
are you sure you've written this sourcecode by yourself? it looks like you have some kind of assignment, and you take others sourcecode and post it here. So we edit and write it for you