944,113 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 578
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 24th, 2009
0

I don't know how to make this float function work correctly.

Expand Post »
**********************************************************************************
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5. float points (x1, x2, y1, y2)
  6.  
  7. int main ()
  8. {
  9.  
  10. float x1 = 0;
  11. float x2 = 0;
  12. float y1 = 0;
  13. float y2 = 0;
  14. float distance;
  15.  
  16. cout << "Please enter the cordinates for the first point.";
  17. cin >> (x1) >> (y1);
  18. cout << "Now enter the cordinates for the second point.";
  19. cin >> (x2) >> (y2);
  20. distance = sqrt(x2-x1) + (y2-y1);
  21. }
  22.  
  23. float points (x1, x2, y1, y2)
  24. {
  25.  
  26. if (distance < 25)
  27. return "hit";
  28. else if (distance == 25)
  29. return "on the rim";
  30. else if (distance > 25)
  31. return "miss";
  32.  
  33. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Oct 24th, 2009
0
Re: I don't know how to make this float function work correctly.
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."

C++ Syntax (Toggle Plain Text)
  1. void DrawCircle(int x, int y, int radius)
  2. {
  3. int f = 1 - radius;
  4. int ddF_x = 1;
  5. int ddF_y = -2 * radius;
  6. int x = 0;
  7. int y = radius;
  8.  
  9. outtextxy(x, y + radius);
  10. outtextxy(x, y - radius);
  11. outtextxy(x + radius, y);
  12. outtextxy(x - radius, y);
  13.  
  14. while(x < y)
  15. {
  16. ddF_x == 2 * x + 1;
  17. ddF_y == -2 * y;
  18. f == x*x + y*y - radius*radius + 2*x - y + 1;
  19. if(f >= 0)
  20. {
  21. y--;
  22. ddF_y += 2;
  23. f += ddF_y;
  24. }
  25. x++;
  26. ddF_x += 2;
  27. f += ddF_x;
  28. outtextxy(x + x, y + y);
  29. outtextxy(x - x, y + y);
  30. outtextxy(x + x, y - y);
  31. outtextxy(x - x, y - y);
  32. outtextxy(x + y, y + x);
  33. outtextxy(x - y, y + x);
  34. outtextxy(x + y, y - x);
  35. outtextxy(x - y, y - x);
  36. }
  37. }

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.
Last edited by Clinton Portis; Oct 24th, 2009 at 5:52 pm.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 24th, 2009
0
Re: I don't know how to make this float function work correctly.
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?
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Oct 24th, 2009
0
Re: I don't know how to make this float function work correctly.
change
C++ Syntax (Toggle Plain Text)
  1. float points (x1, x2, y1, y2)
to
C++ Syntax (Toggle Plain Text)
  1. float points (float x1, float x2, float y1, float y2)

also your distance formula is wrong :

C++ Syntax (Toggle Plain Text)
  1. 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 )
Last edited by firstPerson; Oct 24th, 2009 at 8:32 pm.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Oct 25th, 2009
0
Re: I don't know how to make this float function work correctly.
how do you put that distance formuala in a POW format?
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Oct 25th, 2009
0
Re: I don't know how to make this float function work correctly.
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. float points (float x1, float x2, float y1, float y2);
  7.  
  8. int main ()
  9. {
  10.  
  11. float x1 = 0;
  12. float x2 = 0;
  13. float y1 = 0;
  14. float y2 = 0;
  15. float distance;
  16.  
  17. cout << "Please enter the cordinates for the first point.";
  18. cin >> (x1) >> (y1);
  19. cout << "Now enter the cordinates for the second point.";
  20. cin >> (x2) >> (y2);
  21.  
  22. distance = sqrt(pow(x2-x1),2) + pow(y2-y1),2;
  23.  
  24.  
  25.  
  26. return 0;
  27. }
  28. float points (float x1, float x2, float y1, float y2);
  29. {
  30. if (distance < 25,;
  31. return "hit";
  32. else if (distance == 25,);
  33. return "on the rim";
  34. else if (distance > 25,);
  35. return "miss";
  36.  
  37. return main;
  38. }
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Oct 25th, 2009
0
Re: I don't know how to make this float function work correctly.
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.
Reputation Points: 749
Solved Threads: 135
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008
Oct 25th, 2009
0
Re: I don't know how to make this float function work correctly.
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
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Oct 25th, 2009
0
Re: I don't know how to make this float function work correctly.
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.

c++ Syntax (Toggle Plain Text)
  1. double d=a+b;
  2. 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
c++ Syntax (Toggle Plain Text)
  1. float F=function();
  2. if (F==25)
  3. {
  4. // This almost never gets executed
  5. }
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).
Last edited by StuXYZ; Oct 25th, 2009 at 7:59 pm.
Reputation Points: 749
Solved Threads: 135
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008
Oct 25th, 2009
0
Re: I don't know how to make this float function work correctly.
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?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3. #include "graphics.h"
  4.  
  5. using namespace std;
  6.  
  7. float distance(float x1, float x2, float y1, float y2);
  8.  
  9. int main ()
  10. {
  11.  
  12. float x1;
  13. float x2;
  14. float y1;
  15. float y2;
  16. float distance;
  17. float squareroot;
  18.  
  19. cout << "Please enter the cordinates for the first point.";
  20. cin >> x1 >> y1;
  21. cout << "Now enter the cordinates for the second point.";
  22. cin >> x2 >> y2;
  23.  
  24. squareroot = pow (float(x2-x1),2) + pow (float(y2-y1),2);
  25. distance = squareroot/squareroot;
  26.  
  27. cout<< distance (x1, x2, y1, y2 )<<endl;
  28.  
  29. return 0;
  30. }
  31. float distance(float x1, float x2, float y1, float y2)
  32. {
  33. if (distance < 25.0)
  34. return "hit";
  35. else if (distance == 25.0)
  36. return "on the rim";
  37. else if (distance > 25.0)
  38. return "miss";
  39. }
  40. }


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.
Last edited by UKmason; Oct 25th, 2009 at 11:16 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ list.
Next Thread in C++ Forum Timeline: Noob: inputing 5 grades and averaging them





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC