### Program help

Have your program find which of the y values is closest to 10 (either larger or smaller).
have the program print the x value that gives this closest y value. Also, print how close
the y value is to 10.

 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include using namespace std; int main () { float x, y; cout<<"\t\t\tOutput of First Program!"<0) cout<<"Y is Positive "; if(y<0) cout<<"Y is Negative "; cout<<"\t\t\tX= "<

How do I go about doing these? Could you please give me a hint but not give me the answer. Thanks for help.
Last edited on
So, you have an equation `10 = f(x)` for which you don't have analytic solution.

You have to make a guess; pick some value of x. Calculate f(x). If it is not spot on, then pick a new x and repeat. The question is, how to pick the x wisely so that you do get closer to the correct answer? There are plenty of algorithms for that.
You could have two variables that hold x and y values separate from the ones in your loop and test those. Something like . . .
 ``12345678`` ``````float closestY = y; float closestX = x; if(/*y is closer to 10 than closestY*/) { closestY = y; closestX = x; }``````
Last edited on
Topic archived. No new replies allowed.