Maximum area of quadrilateral in C++

C++Server Side ProgrammingProgramming

Problem statement

Given four sides of quadrilateral a, b, c, d, find the maximum area of the quadrilateral possible from the given sides.

Algorithm

We can use below Brahmagupta’s formula to solve this problem −

√(s-a)(s-b)(s-c)(s-d)

In above formula s is semi-perimeter. It is calculated as follows −

S = (a + b + c + d) / 2

Example

Let us now see an example −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
double getMaxArea(double a, double b, double c, double d) {
   double s = (a + b + c + d) / 2;
   double area = (s - a) * (s - b) * (s - c) * (s - d);
   return sqrt(area);
}
int main() {
   double a = 1, b = 2.5, c = 1.8, d = 2;
   cout << "Maximum area = " << getMaxArea(a, b, c, d) << endl;
   return 0;
}

Output

Maximum area = 3.05
raja
Published on 31-Dec-2019 12:04:23
Advertisements