It is necessary to compile a program for calculating n Heron triangles where the lengths of the sides are consecutive integers and the area too, the problem with calculating the area of ​​a triangle is this: I need to calculate an area for each triangle, that is, for example: I enter n triangles and on the output I want to get:

a=0 b=0 c=0 s=0 ...... a=3 b=4 c=5 s=6 

and I have a, b, c as if accumulating values, and s, too, when entering 5 for example: a = 6 b = 12 c = 18 s = -2147483648

 #include "pch.h" #include <iostream> #include <cmath> using namespace std; int n; int a,b,c,p= 0; int s = 0; int main() { cout << "Vvedite n:"; cin >> n; for (int i = 0; i <= n; i += 1, a += 1, b += 2, c += 3) p = (a + b + c) / 2; s = p * (p - a) * (p - b) * (p - c); s = sqrt(s); cout << "a=" << a << " b=" << b << " c=" << c << " s=" << s << endl; } 
  • Forgot braces after for ? - HolyBlackCat
  • Thanks, but then the next question is, why do I have an area of ​​0 for each triangle when I enter 5? - user310802
  • If you look at the values ​​of the variables in the debugger, you would see that at each iteration p == c obtained, so that multiplication occurs by 0 in * (p - c) . Maybe the formula for calculating the area is wrong? - HolyBlackCat
  • Yes, I saw thanks) - user310802

1 answer 1

Something is plagued me with vague doubts that all your triangles will be degenerate - zero area.

Here is an example for almost equilateral Heronian triangles (you can do other things, but this is perhaps the easiest option):

 #include <iostream> using namespace std; int main() { int n; cin >> n; for(int i = 0, j = 4, last = 2; i < n; ++i, j = j*4 - last, last = (j+last)/4) { int a = j-1, b = j, c = j+1; double s = (a+b+c)/2.0; s = sqrt(s*(sa)*(sb)*(sc)); cout << "a = " << a << ", b = " << b << ", c = " << c << ": s = " << s << "\n"; } } 
  • for (int i = 0, j = 4, last = 2; i <n; ++ i, j = j * 4 - last, last = (j + last) / 4) explain from this line what you are doing here ? - user310802 1:09 pm
  • The link indicates how to count the three consecutive numbers that form the geron triangle - the previous one should be multiplied by 4 and the previous one subtracted. j I have this current value, which for the next number becomes the previous one - so we calculate it j = j*4 - last , the last variable is the previous one ... Save it: last = (j+last)/4 . For the first three 3,4,5 last is equal to 2. - Harry
  • Thank you very much for the decision and for the explanation, I'm sorry I did not carefully read your message) - user310802