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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster

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

 
0
  #1
Oct 24th, 2009
**********************************************************************************
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 329
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 37
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz
 
0
  #2
Oct 24th, 2009
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."

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #3
Oct 24th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,267
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #4
Oct 24th, 2009
change
  1. float points (x1, x2, y1, y2)
to
  1. float points (float x1, float x2, float y1, float y2)

also your distance formula is wrong :

  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.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #5
Oct 25th, 2009
how do you put that distance formuala in a POW format?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #6
Oct 25th, 2009
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
0
  #7
Oct 25th, 2009
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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #8
Oct 25th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
0
  #9
Oct 25th, 2009
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.

  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
  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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #10
Oct 25th, 2009
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?

  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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC