Looks like ye' have done everything except the graphics... so I will offer ye' up a function for drawing an ascii circle.
This code is an algorithm called a, "Bresenham circle."
void DrawCircle(int x, int y, int radius)
{
int f = 1 - radius;
int ddF_x = 1;
int ddF_y = -2 * radius;
int x = 0;
int y = radius;
outtextxy(x, y + radius);
outtextxy(x, y - radius);
outtextxy(x + radius, y);
outtextxy(x - radius, y);
while(x < y)
{
ddF_x == 2 * x + 1;
ddF_y == -2 * y;
f == x*x + y*y - radius*radius + 2*x - y + 1;
if(f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
outtextxy(x + x, y + y);
outtextxy(x - x, y + y);
outtextxy(x + x, y - y);
outtextxy(x - x, y - y);
outtextxy(x + y, y + x);
outtextxy(x - y, y + x);
outtextxy(x + y, y - x);
outtextxy(x - y, y - x);
}
}
Of course, the other alternative would be to create a function where you individually outtextxy( ) every single point of the circle... but this would require a lot of trial and error.
Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
change
float points (x1, x2, y1, y2)
to
float points (float x1, float x2, float y1, float y2)
also your distance formula is wrong :
distance = sqrt(x2-x1) + (y2-y1);
[/code]
the distance between two points is given by the formula :
D = sqrt( (x2-x1)^2 + (y2-y1)^2 )
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
The first problem is that your are returning a string from a function that says it will return a floating point number..
e.g. points has returns "miss" But requires it to return a floating point number.
The next thing is that you have added a comma after each 25 in the function points and a semi colon
e.g. else if (distance == 25,);
BOTH are wrong:
This line needs to be else if (distance==25.0)
Then correct the other three places you have made those two errors.
-----
You HAVE to go back, start again and write MUCH simpler stuff before you try this. It will pay you back because the next assignment will be much much harder unless you grasp the basics, after that you will easily do all the other assingments.
So write
(a) simple program that inputs a number and prints it out.
(b) then a simple program that takes two numbers and adds them together.
(c) a program that uses a function to add those two numbers together.
At every step ALWAY put print out what you are doing, for example
in the program above you should have a cout<<"ENTERING points"<<endl; as the first line of points, and if need be a cout<<"Line 4"<<endl; after each line, and then print the variables etc. I know it sounds trivial and a lot of work but only when you really understand what you are doing at a low level does programming become easy, and then it is just a matter of understanding the algorithm. These simple steps will decide if you do well or badly on this course.
Best of luck.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
If you have a problem like that and don't understand the compiler error, then split the function into bit, e.g. calculate without the sqrt e.g.
double d=a+b;
distance=sqrt(d);
You have three functions in one line, not a problem BUT it is to you because you can't see what the compiler is telling you. So make it simpler and then you will see what the problem is.
The answer to your question is , yes two things. [maybe three depending on your outlook]
Finally, I ment to add that anyone who does this
float F=function();
if (F==25)
{
// This almost never gets executed
}
is asking for nasty trouble,, the reason is that a floating point number is only equal if EVERY bit is exactly the same e.g. 25.000000000001 is NOT equal to 25.0000000000. So think about how to get round that. Additionally, rounding errors in many functions will lose a little accuracy so although the answer should be exactly 25.0 you get 25.0000001 or something like that.
Additionally, although you may chose to have the sqrt function in your code, you can write it without the sqrt function, by observing that the tests like if (sqrt(d)>10.0) is the same as if (d>100.0) (assuming that d is positive, otherwize nasty things happen with the sqrt).
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
Seriously, you are just going round in circles. You HAVE TO WRITE SOMETHING SIMPLER so that you get to grips with variables/ if else constructions / functions. And you are going to have to do it in small pieces.
The current errors include, that you have called the function distance, and then used distance as a variable name (without declaration since line 16 is not in scope).
Line 25 distance = squareroot/squareroot; well
that is the same as distance=1.0;
Line 24 would have been written as this squareroot = pow(x2-x1,2.0) + pow (y2-y1,2.0);
without the horrible c-style cast. (which is unnecessary).
You are still returning a char* when you say you are returning a float.
You don't have ANY cout<<"Variable == "<int main() { return 0;} program and add stuff from there.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
You have a function prototype called distance AND you have a
variable called distance. I am not completely sure but your local variable
should hide the global distance function, or its just an error.
This :
squareroot = pow (float(x2-x1),2) + pow (float(y2-y1),2);
distance = squareroot/squareroot; //will always be 1 or even worse undefined
is wrong, change it to :
squareroot = sqrt ( pow (float(x2-x1),2) + pow (float(y2-y1),2) ); distance = squareroot;
And should you have calculate the distance inside your function instead of in your main like so :
string distance(float x1, float x2, float y1, float y2)
{
float distance = sqrt ( pow( (x2-x1),2) , pow( (y2-y1),2) );
if (distance < 25.0)
return "hit";
else if (distance == 25.0)
return "on the rim";
else if (distance > 25.0)
return "miss";
}
Notice the return type specified, you need to change the prototype as
well to return string.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608