How to calculate the area and perimeter of a triangle on 3 sides? Check whether it is possible to create?

Found and slightly redone this example

#include "stdafx.h" #include "iostream" #include "math.h" #include "conio.h" using namespace std; float qwer(float x1, float x2, float y1, float y2) { float d; d = sqrt(y2 - y1)*(y2 - y1) + (x2 - x1)*(x2 - x1); return d; } float per(float a, float b, float c) { return (a + b + c) / 2; } int main() { setlocale(LC_ALL, "Rus"); float x[3], y[3]; int i; for (i = 0; i < 3; i++) { cout << "Введите координаты " << i + 1 << " точки: "; cin >> x[i] >> y[i]; } float a, b, c; a = qwer(x[0], x[1], y[0], y[1]); b = qwer(x[1], x[2], y[1], y[2]); c = qwer(x[2], x[0], y[2], y[0]); float pt; pt = (a, b, c); cout << "Площадь треугольники" << sqrt(pt*(pt - a)*(pt - b)*(pt - c)) << endl; _getch(); return 0; } 

but after entering the values, that’s what

 Введите координаты 1 точки: 1 2 Введите координаты 2 точки: 3 -1 Введите координаты 3 точки: 2 5 Площадь треугольника-nan(ind) 
  • Something tells me that at least a couple of brackets are missing here: d = sqrt(y2 - y1)*(y2 - y1) + (x2 - x1)*(x2 - x1); And what should pt = (a, b, c); mean pt = (a, b, c); (which actually means pt = c; ) - generally getting lost ... - Harry
  • @Harry pt = c; change and add variable above? float c; - user277573
  • one
    Look at the description of Heron's formula , and correct the jambs in your room. - insolor
  • It will be correct pt=((a,b,c)+(c,a,b)+(b,c,a))/2 - vp_arth
  • And if no joke - in pt = ... just skipped the call to the per function defined above. Brackets - call options just. - vp_arth

2 answers 2

 int main() { double x[3], y[3]; for (int i = 0; i < 3; i++) { cout << "Введите координаты " << i + 1 << " вершины: "; cin >> x[i] >> y[i]; } cout << "Площадь треугольника " << fabs((x[1]-x[0])*(y[2]-y[0])-(x[2]-x[0])*(y[1]-y[0]))/2 << endl; } 

Unless, of course, you are looking at three vertices, as in the source code, and not sideways, as in the question title ...

On both sides:

 int main() { double l[3]; for (int i = 0; i < 3; i++) { cout << "Введите длину " << i + 1 << " стороны: "; cin >> l[i]; } double p = (l[0]+l[1]+l[2])/2; p *= (pl[0])*(pl[1])*(pl[2]); if (p <= 0.0) cout << "Треугольник не существует\n"; else cout << "Площадь треугольника " << sqrt(p) << endl; } 
  • Actually, I’m anybody, but I need it in the form of using functions or classes or methods - user277573
  • one
    @kkkkkkkk As they asked, they answered you that way. If you do n’t give TK, you will get HZ ... WHERE IN YOUR QUESTION is this needed in the form of using functions or classes or methods ? - Harry
  • I did not carefully watch, this is better than my version, so I will not post mine. Before the last output, I think I should add << setprecision (10). - AR Hovsepyan
  • @kkkkkkkk, and you take the code and transfer it to the function. You’ve posted the whole solution, try to do something yourself - AR Hovsepyan
  • @Harry, I don’t say anything like that, thanks for the code - user277573

If you look at the condition of the problem with the text:

The lengths of three sides are given.

  1. We need to check that the triangle exists. A triangle does not exist if the longest side is larger than the sum of the other two. Find the longest side, compare with the sum of the lengths of the other two. Also it would not hurt to check that all lengths are greater than zero. Although, if the triangle does not exist, then the radicand in the Heron's formula will be less or equal to zero, you can check it.
  2. See the description of Heron's formula , find the area of ​​a triangle.
  3. Find the perimeter as the sum of three sides.

The code in question solves another problem: it finds the area by the coordinates of three points. First, the lengths of the sides (a, b, c) are calculated, then the Heron formula is used.

There are two errors in the code:

  • sqrt(y2 - y1)*(y2 - y1) + (x2 - x1)*(x2 - x1) - the square root is taken only from the first bracket. Add generic brackets: sqrt((y2 - y1)*(y2 - y1) + (x2 - x1)*(x2 - x1))
  • pt = (a, b, c); - here it is supposed to call the function of calculating the semi-perimeter (in the code is per ) for later use in the Heron formula. Actually, the function name is missing, it should be like this: pt = per(a, b, c);
    Or more simply: pr = (a + b + c) / 2; The semi-perimeter is calculated once, calculated in one line, why start a separate function for this?