# 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)`
9. `{`
10. `    double submat;`
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[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;`
45. `    mat = x1;`
46. `    mat = y1;`
47. `    mat = 1;`
48. `    mat = x2;`
49. `    mat = y2;`
50. `    mat = 1;`
51. `    mat = x3;`
52. `    mat = y3;`
53. `    mat = 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:

```\$ 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)

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

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