| | |
I don't know how to make this float function work correctly.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 1
**********************************************************************************
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
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)
#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"; }
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."
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.
This code is an algorithm called a, "Bresenham circle."
C++ Syntax (Toggle Plain Text)
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.
Last edited by Clinton Portis; Oct 24th, 2009 at 5:52 pm.
0
#4 Oct 24th, 2009
change to
also your distance formula is wrong :
[/code]
the distance between two points is given by the formula :
D = sqrt( (x2-x1)^2 + (y2-y1)^2 )
C++ Syntax (Toggle Plain Text)
float points (x1, x2, y1, y2)
C++ Syntax (Toggle Plain Text)
float points (float x1, float x2, float y1, float y2)
also your distance formula is wrong :
C++ Syntax (Toggle Plain Text)
distance = sqrt(x2-x1) + (y2-y1);
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?
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 1
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
... 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)
#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; }
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
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
The next thing is that you have added a comma after each 25 in the function points and a semi colon
e.g.
BOTH are wrong:
This line needs to be
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
Best of luck.
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
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 1
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
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
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
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.
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
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
c++ Syntax (Toggle Plain Text)
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
c++ Syntax (Toggle Plain Text)
float F=function(); if (F==25) { // This almost never gets executed }
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
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 1
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?
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.
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)
#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.
Last edited by UKmason; Oct 25th, 2009 at 11:16 pm.
![]() |
Similar Threads
- Help with a time function (PHP)
- advice for this code (C)
- Setting an array and reading in a txt file backwards (C++)
- using Graphic.h (C++)
- Error LU1806 /Windows XP..SP2 ..On Norton (Windows NT / 2000 / XP)
- i can't seem to make my search function work.... help!... (C)
- help needed with errors (C++)
- can anyone assiat in getting my function to work? (C)
Other Threads in the C++ Forum
- Previous Thread: C++ list.
- Next Thread: Noob: inputing 5 grades and averaging them
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






