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.

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.

`#include<stdio.h>`

`#include<stdlib.h>`

`#include<iostream>`

`#include<math.h>`

using namespace std;

double det(int n, double mat[3][3])

`{`

double submat[3][3];

float d;

for (int c = 0; c < n; c++)

`{`

int subi = 0; //submatrix's i value

for (int i = 1; i < n; i++)

`{`

int subj = 0;

for (int j = 0; j < n; j++)

`{`

if (j == c)

continue;

submat[subi][subj] = mat[i][j];

subj++;

`}`

subi++;

`}`

d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat));

`}`

return d;

`}`

int main(int argc, char **argv)

`{`

cout << "Enter the points of the triangle:\n";

int x1, x2, x3, y1, y2, y3;

cin >> x1;

cin >> y1;

cin >> x2;

cin >> y2;

cin >> x3;

cin >> y3;

double mat[3][3];

mat[0][0] = x1;

mat[0][1] = y1;

mat[0][2] = 1;

mat[1][0] = x2;

mat[1][1] = y2;

mat[1][2] = 1;

mat[2][0] = x3;

mat[2][1] = y3;

mat[2][2] = 1;

cout << "\nMatrix formed by the points: \n";

for (int i = 0; i < 3; i++)

`{`

for (int j = 0; j < 3; j++)

cout << mat[i][j] << " ";

cout << endl;

`}`

float determinant = det(3, mat)*0.5;

if (determinant < 0)

cout << "The Area of the triangle formed by (" << x1 << "," << y1

<< "), (" << x2 << "," << y2 << "), (" << x3 << "," << y3

<< ") = " << (determinant * -1);

`else`

cout << "The Area of the triangle formed by (" << x1 << "," << y1

<< "), (" << x2 << "," << y2 << "), (" << x3 << "," << y3

<< ") = " << determinant;

return 0;

`}`

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.**

advertisement

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