I need some instructions on this problem

Hi everyone, I have a problem to solve, but I have no idea where to start. If anyone can help me with a step-by-step description of what I have to do it will be perfect... just an algorithm to follow:)
Ok, here's the problem :

"You have to write a program in C++ that computes the difference between the maximal and the minimal value of function f(x,y) obtained on an integer point in a given rectangle [a, b] x [c, d]. Your program should prompt the user to input numerical values of a, b, c and d, as floating point numbers, which are expected to be in a range from −100 thru 100. In case the minimal or maximal values do not exists, your program should output appropriate messages. "

a=5, b=7, c=-7, d=10, f(x,y)=(y+x*x)*(1+x*x*x);

Thanks a lot! ;)
Last edited on
I don't get the question. To simplify it, are you supposed to find the max and min of f(x,y) where 5<x<7, and -7<y<10 (in that example)?

If that's so, then it's quite easy, given how the question states that you're only supposed to look through the integer points.

So, the process should be something like this:
1
2
3
4
5
6
7
Get the values of a,b,c,d
Make a double-for() loop, one going from a->b (or b->a, depending on which is greater) and the other
from c->d (or d->c). The first counter is the x value and the second is the y value. Go running through
each series of values ([5,-7],[5,-6],[5,-5]...[5,10],[6,-7],[6,-6]...) and checking to see if it's the largest
or smallest value found so far (with if() statements to save the new value should it be larger or smaller
than the previous max or min value).
Ta-dah! 
Last edited on
Yeah, I don't get it too ... here is what I've done so far :


#include <iostream>
using namespace std;

int main()
{
double a,b,c,d,x,y,f;

cout<<"Please, enter four numbers:"<<"\n";
cout<<"a =";
cin>>a;

cout<<"b =";
cin>>b;

cout<<"c =";
cin>>c;

cout<<"d =";
cin>>d;
if (a>100 || b>100 || c>100 || d>100 || a<-100 || b<-100 || c<-100 || d<-100)
{
cout << "The entered numbers must be in a range from −100 thru 100 ";
}
else
{
for ( x=a; x<=b; x=x+0.01)
{
for ( y=c; y<=d; y=y+0.01)

f=(y+x*x)*(1+x*x*x);

cout<<"f= "<<f<<"\n";

}
}
return 0;
}


now the only problem is to find the min and the max of the function f
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// MIN
// Initialize min outside the for loop. you can take the very first value of f.
min=someInitialValue;
// Check this each time you calculate f.
if (min>f)
{
    min=f;
}


// Almoset the same with MAX
max=someInitialValue;
if (max<f)
{
    max=f;
}


here you are :-)
Topic archived. No new replies allowed.