c++ Any circle is completely determined by its center (h, k) and radius r (x-h)^
ID: 3605966 • Letter: C
Question
c++
Any circle is completely determined by its center (h, k) and radius r (x-h)^2 + (y-k)^2 r^2 = a Define a structure that models the data needed to determine a circle and call it circle b. Instantiate two Circle objects and allow the user to populate the data c. Write a function that displays a circle. With prototype, void printcircle (Circle arg); The cout in the function should string together a nice output. For example, sample output might look like (x-3)^2 + (y+2)^2= 25 and (x+5)^2 + (y-4)^2 16 = Run your program several times with different values for h, k and r. Be sure to account for any values for h, k and/or r that do not make sense. For example.-7. (In other words, perform some data validation here.)Explanation / Answer
A structure Circle is created like:
struct Circle
{
int xcenter;
int ycenter;
int radius;
};
2 objects are then created:
Circle circle1;
Circle circle2;
Then user input is taken using the function input()
and validations aredonr for negative radius.
Then whether the co ordinates are +ve or -ve cout displays it in a proper format in the printCircle function.
I hope its clear. If you have any more doubts, you ca comment below. All the best. :)
**********************************************************************************************************************
Code:
#include <iostream>
using namespace std;
struct Circle
{
int xcenter;
int ycenter;
int radius;
};
int input()
{
int data;
cin >> data;
return data;
}
void printCircle(Circle arg)
{
if(arg.xcenter<0)
cout << "(x+" << -arg.xcenter << ")^2 + ";
else if(arg.xcenter>0)
cout << "(x-" << arg.xcenter << ")^2 + ";
else
cout << "x" << "^2 + ";
if(arg.ycenter<0)
cout << "(y+" << -arg.ycenter << ")^2 = ";
else if(arg.ycenter>0)
cout << "(y-" << arg.ycenter << ")^2 = ";
else
cout << "y" << "^2 = ";
cout << arg.radius*arg.radius << " ";
}
int main()
{
Circle circle1;
Circle circle2;
cout << "Enter data for Circle 1" << endl;
cout << "Enter the x coordinate for the center:" << endl;
circle1.xcenter=input();
cout << "Enter the y coordinate for the center:" << endl;
circle1.ycenter=input();
cout << "Enter the radius:" << endl;
circle1.radius=input();
while(circle1.radius<=0)
{
cout << "Radius of circle must be positive. Please enter again." << endl;
circle1.radius=input();
}
cout << "Enter data for Circle 2" << endl;
cout << "Enter the x coordinate for the center:" << endl;
circle2.xcenter=input();
cout << "Enter the y coordinate for the center:" << endl;
circle2.ycenter=input();
cout << "Enter the radius:" << endl;
circle2.radius=input();
while(circle2.radius<=0)
{
cout << "Radius of circle must be positive. Please enter again." << endl;
circle2.radius=input();
}
printCircle(circle1);
printCircle(circle2);
}
********************************************************************************************************************************
Sample Outputs:
**********************************************************************************************************************************
I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.