C++ Program to Find the Area of a Triangle Using Determinants

«
»
This is a C++ program to find the area of triangle using determinants.
Formula for the area of a triangle using determinants
x1 y1 1
Area=±1/2 x2 y2 1
x3 y3 1
The plus/minus in this case is meant to take whichever sign is needed so the answer is positive (non-negative). Do not say the area is both positive and negative.

Here is source code of the C++ Program to Compute the Area of a Triangle Using Determinants. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<iostream>
  4. #include<math.h>
  5.  
  6. using namespace std;
  7.  
  8. double det(int n, double mat[3][3])
  9. {
  10.     double submat[3][3];
  11.     float d;
  12.     for (int c = 0; c < n; c++)
  13.     {
  14.         int subi = 0; //submatrix's i value
  15.         for (int i = 1; i < n; i++)
  16.         {
  17.             int subj = 0;
  18.             for (int j = 0; j < n; j++)
  19.             {
  20.                 if (j == c)
  21.                     continue;
  22.                 submat[subi][subj] = mat[i][j];
  23.                 subj++;
  24.             }
  25.             subi++;
  26.  
  27.         }
  28.         d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat));
  29.     }
  30.     return d;
  31. }
  32.  
  33. int main(int argc, char **argv)
  34. {
  35.  
  36.     cout << "Enter the points of the triangle:\n";
  37.     int x1, x2, x3, y1, y2, y3;
  38.     cin >> x1;
  39.     cin >> y1;
  40.     cin >> x2;
  41.     cin >> y2;
  42.     cin >> x3;
  43.     cin >> y3;
  44.     double mat[3][3];
  45.     mat[0][0] = x1;
  46.     mat[0][1] = y1;
  47.     mat[0][2] = 1;
  48.     mat[1][0] = x2;
  49.     mat[1][1] = y2;
  50.     mat[1][2] = 1;
  51.     mat[2][0] = x3;
  52.     mat[2][1] = y3;
  53.     mat[2][2] = 1;
  54.  
  55.     cout << "\nMatrix formed by the points: \n";
  56.     for (int i = 0; i < 3; i++)
  57.     {
  58.         for (int j = 0; j < 3; j++)
  59.             cout << mat[i][j] << " ";
  60.         cout << endl;
  61.     }
  62.  
  63.     float determinant = det(3, mat)*0.5;
  64.     if (determinant < 0)
  65.         cout << "The Area of the triangle formed by (" << x1 << "," << y1
  66.                 << "), (" << x2 << "," << y2 << "), (" << x3 << "," << y3
  67.                 << ") = " << (determinant * -1);
  68.     else
  69.         cout << "The Area of the triangle formed by (" << x1 << "," << y1
  70.                 << "), (" << x2 << "," << y2 << "), (" << x3 << "," << y3
  71.                 << ") = " << determinant;
  72.     return 0;
  73. }

Output:

advertisement
$ g++ TriangleArea.cpp
$ a.out
 
Enter the points of the triangle:
3 4
6 4
3 9
 
Matrix formed by the points: 
3 4 1 
6 4 1 
3 9 1 
The Area of the triangle formed by (3,4), (6,4), (3,9) = 7.5
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.