Need help finding the radius of my circle..

Hi guys, I have been looking around the forum for a little while and I love this place. I can't seem to find my answer though so I decided to make my own post.
Details:
-I am using x code
-I am trying to find the distance between 2 points
-I keep getting "Non-ASCII characters are allowed outside of literals and identifiers (I will point out where)
-There is one more thing I will point out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <cmath>
#include <iostream>

using namespace std;

int main()
{
    float mpx;
    float mpy;
    int x1;
    int y1;
    int x2;
    int y2;
    int cpx;
    int cpy;
    int radius;
    srand(time(0));
    rand();
    
    cout << "Please enter the location of your circle\'s midpoint." << endl <<endl;
    cout << "Enter the x coordinate of your circle\'s midpoint (-99 to 99): ";
    cin >> mpx;
    cout << "Enter the y coordinate of your circle\'s midpoint (-99 to 99): ";
    cin >> mpy;
    cout << endl;
    
    x1 = rand() % 199 - 99;
    y1 = rand() % 199 - 99;
    x2 = rand() % 199 - 99;
    y2 = rand() % 199 - 99; /*This random always goes above and beyond 199 - 99
 and it is killing me, I just cannot figure out why!*/
    
    cout << "The location of a point on your circle\'s ciricumference has been randomly set to: ";
    //Displays first x coordinate
    cout << "\nx1 coordinate: " <<x1;
    //Displays first y coordinate
    cout << "\ny1 coordinate: " <<y1;
    //Displays second x coordiante
    cout << "\nx2 coordinate: " <<x2;
    //Displays second y coordinate
    cout << "\ny2 coordinate: " <<y2;
    //Displays the length of the circle's radius using √((p0x – p1x)2 + (p0y – p1y)2)

    cout << "The length of the radius of the circle is " << sqrt((x1 – x2)*2) + (y1 – y2)*2)) /* 
This is where I keep getting my problem... 
In xcode, it keeps saying (and pointing to)
 Non-ASCII characters are not allowed outside of literals and identifiers. 
It points to the 2 on the end of the parenthesis.
It also says "character expected" and shows a close parenthesis. 

    //Displays the circle's area using
    cout << "The area of the circle with a radius of # is #" <<endl;
    
    return EXIT_SUCCESS;
    
}
 */


Thanks in advance guys, I appreciate it!
You have mismatched parentheses on line 44 and your on the same line is not an ascii character.
Last edited on
Besides, you want the square of the difference, not "times two".
Thanks guys, how would i use pow()? I get an error for that too... I also tried ^2. Sorry for the noob questions... I put || instead of the minus because -- gives me an error as well! Isn't that for boolean though?

Thanks again
I have an example using pow function,
hope this help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//pow.cpp
//Use pow function.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cmath>
using std::pow;

int main(){

double number;

cout<<"Enter a number: ";
cin>>number;

cout<<number<<"^2 is: "<<pow(number,2)<<endl;

return 0; //indicates success
}//end main 



Eyenrique-MacBook-Pro:Help Eyenrique$ ./pow 
Enter a number: 3
3^2 is: 9
Eyenrique-MacBook-Pro:Help Eyenrique$ ./pow 
Enter a number: 4
4^2 is: 16
Eyenrique-MacBook-Pro:Help Eyenrique$ ./pow 
Enter a number: 2
2^2 is: 4


You have to use double type variables.
Last edited on
You could do either
(x1 - x2)*(x1 - x2)
or
pow((x1 - x2), 2.0)

The ^ operator is something else entirely, its one of the bitwise operators, used for manipulating integers at the bit level.

I put || instead of the minus because -- gives me an error as well!

You need to use - for minus. I suspect you are having problems due to using a word processor for editing code (well, I'm guessing here). You need to use either a plain text editor (notepad etc) or a proper code editor.
Please do not use pow() to square stuff.

Talk about using a sledgehammer to swat a fly.
poor fly Dx
by the way thanks for advice @Disch
Last edited on
Talk about using a sledgehammer to swat a fly.


LOL... So I should use
(x1 - x2)*(x1 - x2)
?

You need to use - for minus. I suspect you are having problems due to using a word processor for editing code (well, I'm guessing here). You need to use either a plain text editor (notepad etc) or a proper code editor.


I am doing everything directly within xcode.. I just don't understand why
-
won't work... Do you think it is because I tried to power wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
    cout << "The location of a point on your circle\'s ciricumference has been randomly set to: ";
    //Displays first x and y coordinates
    cout << "\n(" <<x1 << ", " << y1 << ")";
    //Displays second x and y coordinates
    cout << "\n(" <<x2 << ", " << y2 << ")" <<endl;
    //Displays the length of the circle's radius using √((p0x – p1x)2 + (p0y – p1y)2) divided by 2
    cout << "\nThe length of the radius of the circle is " << sqrt((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)) << endl;
    //Displays the circle's area using
    cout << "\nThe area of the circle with a radius of # is #" <<endl;
    
    return EXIT_SUCCESS;
    
}


Ended up with this
Please enter the location of your circle's midpoint.

Enter the x coordinate of your circle's midpoint (-99 to 99): 1
Enter the y coordinate of your circle's midpoint (-99 to 99): 1

The location of a point on your circle's ciricumference has been randomly set to: 
(54, -2)
(11, -43)

The length of the radius of the circle is 1724

The area of the circle with a radius of # is #
Program ended with exit code: 0


The radius just seem erroneous..
The original code (displayed on this forum) contains a unicode character
UTF-8: E2 80 93
which is named "en dash"
This character doesn't normally appear on the keyboard, but it may be auto-generated by some editors. (such editors are unsuitable for writing code).
Last edited on
I actually got it to work! Thanks, my only problem left is the number I end up with.. Is sqrt((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)) really giving me a true answer? Or am I not truly getting the answer I am looking for? Because the number I get is outrageous!

Thanks

EDIT: I tried pow() and it worked... Curious why the other way around didn't though. Thanks a ton guys! :)
Last edited on
Well, lets look at the sqrt((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1))

Add some syntactic sugar:
1
2
3
4
5
6
dx = (x2 - x1)
dy = (y2 - y1)
// replace in equation:
sqrt( dx*dx )            + (dy*dy)
// which is logically same as:
abs(dx) + dy*dy

cire did already mention the mismatching parentheses of the OP version. I bet you did fix them in one way for multiplication and the other way for pow().

At least some editors can conveniently show, which parentheses do match.
Or am I not truly getting the answer I am looking for? Because the number I get is outrageous!

The radius is the distance between the centre (mpx, mpy) and a point (x, y) on the circumference.

However, you seem to be calculating the distance between two different points and ignoring the centre. I don't know whether that affects your expected result, or if I misunderstood what you are aiming to achieve.
Topic archived. No new replies allowed.