Write a program that reads radius of a circle where its center is located on the
ID: 3579426 • Letter: W
Question
Write a program that reads radius of a circle where its center is located on the origin. Then it prompts the user to enter x and y coordinates of several points and displays the distance between each point and the center of the circle. The program will stop only if a point is located inside the circle. The concept is shown in the following figure. The distance d can be calculated using the following d = squareroot (x_2 - x_1)^2 + (y_2 - y_1)^2 A sample output of the program is shown below. Enter the radius: 10 Enter the coordinates of the 1st point (x, y): 13 -12 d = 17.69, the point is located outside Enter coordinates of the next point (x, y): 20 3 d = 20.22, the point is located outside Enter coordinates of the next point (x, y): 5 6 d = 7.81, the point is located inside...the program will now closeExplanation / Answer
//Tested on Ubuntu,Linux
/**********************cord.cpp****************************/
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
//main function start
int main() {
//variable declarations
int x,y,r;
int first=1;
float d;
//prompt for radius
std::cout<<"Enter the radius:";
std::cin>>r;
while(true) {
//prompt for coordinates
if(first==1){
std::cout<<"Enter the coordinates of the 1st point(x,y):";
std::cin>>x>>y;
}
else{
std::cout<<"Enter the coordinates of the next point(x,y):";
std::cin>>x>>y;
}
//calculating distance d=sqrt(x2-x1)^2+(y2-y1)^2)
// center coordinates is 0,0
//loop break when d is less then radius
first++;
d=sqrt((x-0)*(x-0)+(y-0)*(y-0));
//for setting precision
std::cout << fixed << showpoint;
std::cout << setprecision(2);
//checking point is outside or inside the circle
if(d>r) {
std::cout<<"d = "<<d<<", the point is located outside "<<std::endl;
}else if(d<r) {
std::cout<<"d = "<<d<<", the point is located inside... the program will now close "<<std::endl;
break;
}else {
std::cout<<"d = "<<d<<", the point is located on circumtences "<<std::endl;
}
}
return 0;
}
//main function End
/******************output*******************/
lalchand@lalchand:~/Desktop/chegg$ g++ cord.cpp -lm
lalchand@lalchand:~/Desktop/chegg$ ./a.out
Enter the radius:10
Enter the coordinates of the 1st point(x,y):13 -12
d = 17.69, the point is located outside
Enter the coordinates of the next point(x,y):20 3
d = 20.22, the point is located outside
Enter the coordinates of the next point(x,y):5 6
d = 7.81, the point is located inside... the program will now close
Thanks a lot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.