**********************************************************************************
Write a float function that will have four float parameters. The four parameters represent two Cartesian points, 2 x's and 2 y's. Return the distance between the two points. It involves a square root.
Write a main function which will ask the user for 2 points (4 numbers), decide whether the distance between them is within 25 units ("hit"), exactly 25 units ("on the rim"), or more than 25 units away ("missed"). You must call the function written previously. Use the fewest possible number of if statements and comparisons to do this efficiently. Output this message on the console window (use "cout").

Then add in some code to use the first point entered as the center of a circle, and draw a circle that is 25 units in radius. Draw a small circle at the location of the other point. Output the classification of the distance on the graphics screen (look up the function "outtextxy".). The use of the word "units" does not imply any conversions needed, they are just 'spaces on the plane' or pixels in the graphics sense

*********************************************************************************

this is what i have so far

#include <iostream>
#include <cmath>

using namespace std;
float points (x1, x2, y1, y2)

int main ()
{

	float x1 = 0;
	float x2 = 0;
	float y1 = 0;
	float y2 = 0;
	float distance; 

	cout << "Please enter the cordinates for the first point.";
	cin >> (x1) >> (y1);
	cout << "Now enter the cordinates for the second point.";
	cin >> (x2) >> (y2);
	distance = sqrt(x2-x1) + (y2-y1);
}
	
float points (x1, x2, y1, y2)
{

	if (distance < 25)
		return "hit";
	else if (distance == 25)
		return "on the rim";
	else if (distance > 25)
		return "miss";

}

Recommended Answers

All 11 Replies

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.

I dont think i'm ready for the graphics yet... what I have right now will not compile... have any idea what should be done to fix that?

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 )

how do you put that distance formuala in a POW format?

I've been working on this for like a week now and I still can't make it work :(...

This is what I have now

If you have any idead of what I need to do please let me know, Any help at all is greatly appreciated

#include <iostream>
#include <cmath>

using namespace std;

float points (float x1, float x2, float y1, float y2);

int main ()
{

	float x1 = 0;
	float x2 = 0;
	float y1 = 0;
	float y2 = 0;
	float distance; 

	cout << "Please enter the cordinates for the first point.";
	cin >> (x1) >> (y1);
	cout << "Now enter the cordinates for the second point.";
	cin >> (x2) >> (y2);
	
	distance = sqrt(pow(x2-x1),2) + pow(y2-y1),2;

	

	return 0;
}
float points (float x1, float x2, float y1, float y2);
{
	if (distance < 25,;
		return "hit";
	else if (distance == 25,);
		return "on the rim";
	else if (distance > 25,);
		return "miss";

	return main;
}

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.

I think I'm gettin it now, except I have one last error that I cant figure out... maybe Im thinking too hard.

Is there anything wrong with-

distance = sqrt((pow(x2-x1),2.0) + pow(y2-y1),2.0)

it keeps telling me that both pows are

error C2661: 'pow' : no overloaded function takes 1 argument
error C2661: 'pow' : no overloaded function takes 1 argument

and the sqrt is

error C2661: 'sqrt' : no overloaded function takes 2 arguments

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).

Ive been emailing my TA and have gotten this far...

I need to make this work somehow but Im soo lost... any way to fix this to where Im done?

#include <iostream>
#include <cmath>
#include "graphics.h"

using namespace std;

float distance(float x1, float x2, float y1, float y2);

int main ()
{

	float x1;
	float x2;
	float y1;
	float y2;
	float distance;
	float squareroot; 

	cout << "Please enter the cordinates for the first point.";
	cin >> x1 >> y1;
	cout << "Now enter the cordinates for the second point.";
	cin >> x2 >> y2;
	
	squareroot = pow (float(x2-x1),2) + pow (float(y2-y1),2);
	distance = squareroot/squareroot;
	
	cout<< distance (x1, x2, y1, y2 )<<endl;

	return 0;
}
float distance(float x1, float x2, float y1, float y2)
{
	if (distance < 25.0)
		return "hit";
	else if (distance == 25.0)
		return "on the rim";
	else if (distance > 25.0)
		return "miss";
}
}

I get this error on lines 35,41,42
error C2143: syntax error : missing ',' before ')

I need this.............


Write a main function which will ask the user for 2 points (4 numbers), decide whether the distance between them is within 25 units ("hit"), exactly 25 units ("on the rim"), or more than 25 units away ("missed"). You must call the function written previously. Use the fewest possible number of if statements and comparisons to do this efficiently. Output this message on the console window (use "cout").
Then add in some code to use the first point entered as the center of a circle, and draw a circle that is 25 units in radius. Draw a small circle at the location of the other point. Output the classification of the distance on the graphics screen (look up the function "outtextxy".). The use of the word "units" does not imply any conversions needed, they are just 'spaces on the plane' or pixels in the graphics sense.

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 == "<<var<<endl; or anything else to actually help figure out the steps that your program is taking.

It will take a complete novice, a couple of hours to build this program up from basics, piece by piece BUT if you insist of trying to do it all in one go, you are going to be here are very very long time. The easiest way to program is alway to make a small modification to a working program that you understand. In your case. start with just the int main() { return 0;} program and add stuff from there.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.