Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Prints the central coordinate and the radius of a circumscribed circle of a triangle

C++ Basic: Exercise-67 with Solution

Write a C++ program to which prints the central coordinate and the radius of a circumscribed circle of a triangle which is created by three points on the plane surface.

Pictorial Presentation:

C++ Exercises: Prints the central coordinate and the radius of a circumscribed circle of a triangle

Sample Solution:

C++ Code :

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
   double a, b, c, x1, y1, x2, y2, x3, y3, xp, yp, d, radius;
    while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3) {
            a = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
            b = sqrt((x3 - x1)*(x3 - x1) + (y3 - y1)*(y3 - y1));
            c = sqrt((x3 - x2)*(x3 - x2) + (y3 - y2)*(y3 - y2));
            radius = (a*b*c) / (sqrt((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c)));
            d = 2*(x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2));
            xp = ((x1*x1 + y1*y1)*(y2-y3) + (x2*x2 + y2*y2)*(y3-y1) + (x3*x3 + y3*y3)*(y1-y2))/d;
            yp = ((x1*x1 + y1*y1)*(x3-x2) + (x2*x2 + y2*y2)*(x1-x3) + (x3*x3 + y3*y3)*(x2-x1))/d;
            cout << fixed << setprecision(3) << "Central coordinate of the circumscribed circle: (" << xp << ", " << yp << ")\nRadius: " << radius << endl;
         }
   	return 0;
}

Sample Output:

Sample input : 6 9 7
Sample Output:
7 9 6

Flowchart:

Flowchart:  Prints the central coordinate and the radius of a circumscribed circle of a triangle

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to add all the numbers from 1 to a given number.
Next: Write a C++ program to read seven numbers and sorts them in descending order.

What is the difficulty level of this exercise?