find correct answer (c++ program)

Hi
please check code and tell me my error

Write a program in C++ that computes the minimal and the maximal 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 when minimal or maximal values do not exists, your program should output appropriate messages.
f(x,y)=2-x+7*x*x-x*x*x;




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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=2-x+7*x*x-x*x*x;

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

}
}
return 0;
}






answer is not showing minimum answer... just showing maximum
Last edited on
You aren't storing the minimum or maximum value. Looks to me like you're just outputting the value of f every time you calculate it. Also, what do you think your second for loop is doing? It does not have any statements following it in braces (i.e. no {...} after your second for statement). This means that only the first statement following it gets executed within the loop; is that what you meant to do?

The problem isn't really your code; the problem is that you haven't thought enough about what you want to do with the numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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=2-x+7*x*x-x*x*x;
      cout<<"f= "<<f<<"\n";
    }
   }
return 0;
}

Last edited on
Topic archived. No new replies allowed.