Ok, so i have an assigment to find the minimal and maximal values of a function in a sector, determined by a rectangle. I happen to solve the problem under borland c++, but when i try to adapt the code to dev cpp i receive a lot of mistakes. The code is:
#include <iostream>
using namespace std;
float a, b, c, d;
int left, right, top, bottom;
int min=32000, max=-32000;
int minfound=0, maxfound=0;
int atx1, atx2;
int f(int x)
{ return x*x+x-2; }
void prompt( )
{
cout<<"You are about to calculate the extrema of the function";
cout<<"\ny = x^2 + x - 2 obtained in an integer point of the";
cout<<"\nrectangle [a,b]x[c,d]";
cout<<"\n\n Enter the coordinates of the upper right point of the rectangle,";
cout<<"\nwhich must be in a range from -100 to 100 >> ";
cin >> a >> b;
cout<<"\nEnter the coordinates of the bottom left point of the rectangle,";
cout<<"\nwhich must be in a range from -100 to 100 >> " ;
cin >> c >> d;
if (a > c) {left = c; right = a;}
else {left = a; right = c;}
if (b > d) {top = b; bottom = d;}
else {top = d; bottom = b;}
the majority of erros are connected to the declaration of variables. example:
'left' undeclared (first use this function)
'right' undeclared (first use this function)
this also applies for max and min
and there are a coupe of [warnings] that i am converting from int to float(which i think it's legal and should not compromise the final result of the program )
So, can anyone pinpoint why do i encounter these mistakes and how to fix them ?10x
Ah-ha! This is why I don't use using namespace.
There are two 'left's declared here: yours, and std::left.
You can solve the conflict by renaming your variable, or by removing the using namespace and updating the couts.
On a different subject, doing this in a professional environment is inadvisable:
1 2
int f(int x)
{ return x*x+x-2; }
1 2
if (a > c) {left = c; right = a;}
else {left = a; right = c;}
if ((check(y)) && (y <= min)) {*p = y; atx1=x; minfound=1;}
And I'm pretty sure none of the globals you declared should be global. Oh sure, globals seem like a silver bullet when you're a beginner, saving you from explicitly passing parameters, but you can trust me when I say you'll regret it later. As a matter of fact, you're regretting it right now. Just look at your question itself!
can u explain it in a simpler way? yes, i'm a begginer and i wasn't able to understand your reply. what do u mean by 'updating couts' and where does this std::left come from? i would appreciate if you could show my sections of code which i have to alter
there are already declarations of left and right in the stl.
You must either rename your left and right variables, or as he said, remove the line 'using namespace std' and then put std:: in front of every use of cout and every other iostream operator.
A third more dodgy way to solve it would be to remove 'using namespace std;' and change iostream to iostream.h. You'll get a warning about a depricated header, and its not really a good solution... just faster.
#include <iostream>
usingnamespace std;
float a, b, c, d;
float _left, _right, top, bottom;//change to float and add _
int min=32000, max=-32000;
int minfound=0, maxfound=0;
int atx1, atx2;
int f(int x)//f and x ints to floats
{ return x*x+x-2; }
void prompt( )
{
cout<<"You are about to calculate the extrema of the function";
cout<<"\ny = x^2 + x - 2 obtained in an integer point of the";
cout<<"\nrectangle [a,b]x[c,d]";
cout<<"\n\n Enter the coordinates of the upper right point of the rectangle,";
cout<<"\nwhich must be in a range from -100 to 100 >> ";
cin >> a >> b;
cout<<"\nEnter the coordinates of the bottom left point of the rectangle,";
cout<<"\nwhich must be in a range from -100 to 100 >> " ;
cin >> c >> d;
if (a > c) {_left = c; _right = a;}
else {_left = a; _right = c;}
if (b > d) {top = b; bottom = d;}
else {top = d; bottom = b;}
//cout << "Left bottom corner "<<left << " " <<bottom<<endl;
//cout << "Right top corner " << right << " " << top<<endl;
}
int check(int y )
{ if ((y>=bottom)&&(y<=top)) return 1;
elsereturn 0;
}
void extrema(int *p, int *q)
{
int y;
for (int x=_left; x<=_right; x++)//dropped int
{int min,max;//declare min and max
y=f(x);
if ((check(y)) && (y <= min)) {*p = y; atx1=x; minfound=1;}
if ((check(y)) && (y >= max)) {*q = y; atx2=x; maxfound=1;}
}
}
void output( )
{int min,max;//declare min and max
if (minfound) cout<<"\nMinimal = "<<min<<" at ("<<atx1<<","<<min<<")";
else cout<<"\nMinimal value doesn't exist.";
if (maxfound) cout<<"\nMaximal = "<<max<<" at ("<<atx2<<","<<max<<")";
else cout<<"\nMaximal value doesn't exist.";
}
int main( )
{int min,max;//declare min and max
prompt( );
extrema(&min, &max);
output( );
cin.get;
return 1;
}
. . . .with a warning on line 48 because of conversion of a float to an int
This is what it prints... still some work to do!
You are about to calculate the extrema of the function
y = x^2 + x - 2 obtained in an integer point of the
rectangle [a,b]x[c,d]
Enter the coordinates of the upper right point of the rectangle,
which must be in a range from -100 to 100 >> 50 25
Enter the coordinates of the bottom left point of the rectangle,
which must be in a range from -100 to 100 >> 10 5
Minimal value doesn't exist.
Maximal value doesn't exist.
Press any key to continue . . .