It is necessary to write a function that calculates the length of the segment according to the coordinates of its ends. Using this function, calculate the perimeter of a triangle using the coordinates of its three vertices. All numbers do not exceed 30,000 in absolute value. The perimeter value of this triangle with an accuracy of up to 6 digits after the decimal point. Here is my program, passes 25/32 tests. What needs to be fixed?

#include <iostream> #include <cmath> double L (int x, int y ,int x1, int y1) { return sqrt( ( x- x1 )*( x- x1 ) + ( y-y1 )*( y-y1 )); } long int a,b,c,d,e,f; double s1,s2,s3,s,p,P; using namespace std; int main() { cin>>a>>b>>c>>d>>e>>f; s1=L(a,b,c,d); s2=L(c,d,e,f); s3=L(e,f,a,b); P=s1+s2+s3; cout<<P; return 0; } 
  • And the coordinates of the input - integers? - Vladimir Martyanov
  • Yes, integers - Julia Ponomareva
  • one
    "up to 6 decimal places" - where? - Igor
  • one
    x1 = 30000, x2 = -30000 - what will be under the root? if there was an unsigned int, then it would have been laid out ... and so 3.6 billion does not seem to fit in - Vladimir
  • one
    Compare: long int a, b, c, d, e, f; and for some reason double L ( int x, int y, int x1, int y1) - Akina

1 answer 1

Julia, try this instead of your L () function and run tests with it:

 double L (int x, int y ,int x1, int y1) { double x2 = (double)( x-x1 )*( x-x1 ); double y2 = (double)( y-y1 )*( y-y1 ); return sqrt( x2 + y2 ); } 

PS: yes, the type conversion could be set up under sqrt (). But so clearer. And instead of double for intermediate values, you can take a long int .

PPS: yes, in modern C ++ it is customary to give types in other words. But it is more compact and "does not affect the speed."

PPPS: and again, since you have a restriction on input values, you should check them as you type.

  • ghost type must be done before multiplication ... In general, and in the function to betray ... - Fat-Zer
  • 2
    @ Fat-Zer, in this case it will be done BEFORE multiplication. As far as I remember, this type conversion refers to the operand following it, and not to the entire line. About the function - yes, it would be good to transmit immediately But the task may be a restriction on the arguments - we were not informed about this. So I changed the minimum. - Vladimir
  • Yes, I'm sorry ... I pereklinilo ... - Fat-Zer