check my mistakes and tell me please
Jan 12, 2011 at 3:02pm UTC
Hi programmers
please check code and tell me my error
why is it not showing values on input of some numbers.
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.
a=1, b=10, c=1, d=-20, 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 41 42 43 44 45 46 47 48 49
#include <iostream>
using namespace std;
int main()
{
double a,b,c,d,x,y,f;
int min=0;
int max=0;
cout<<"Please, enter values for a,b,c and d:" <<"\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 << "Bad input. The numbers must be in the interval [-100, 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;
if (min>f) f=min;
if (max<f) f=max;
cout<<"f = " <<f<<"\n" ;
}
}
cout << "f(min) = " << min << "\n" ;
cout << "f(max) = " << max << "\n" ;
return 0;
}
i am waiting
Jan 12, 2011 at 3:24pm UTC
I not exactly sure what you're asking, but this looks wrong:
1 2
if (min>f) f=min;
if (max<f) f=max;
I think it ought to be 'min = f' rather than 'f = min'
Last edited on Jan 12, 2011 at 3:24pm UTC
Jan 12, 2011 at 4:00pm UTC
I recommend all programmers adopt a fanatical devotion to unit testing and test-driven development.
Jan 12, 2011 at 4:06pm UTC
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 41 42 43
#include <iostream>
using namespace std;
int main()
{
double a,b,c,d,x,y,f;
int min=0;
int max=0;
cout<<"Please, enter values for a,b,c and d:" <<"\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 << "Bad input. The numbers must be in the interval [-100, 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" ;
}
}
cout << "f(min) = " << min << "\n" ;
cout << "f(max) = " << max << "\n" ;
return 0;
}
not showing correct errors.
please any one can check ......
Jan 12, 2011 at 5:41pm UTC
Last edited on Jan 12, 2011 at 5:43pm UTC
Topic archived. No new replies allowed.