D
D
dikxarper2020-06-17 18:45:11
C++ / C#
dikxarper, 2020-06-17 18:45:11

What mistakes did I make in the code?

Exercise
Определите тип треугольника (остроугольный, тупоугольный, прямоугольный) с данными сторонами. Входные данные: три натуральных числа – стороны треугольника. Выходные данные: одно из слов rectangular для прямоугольного треугольника, acute для остроугольного треугольника, obtuse для тупоугольного треугольника или impossible, если входные числа не образуют треугольника.

My code:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  double a,b,c,x,y,z;
  cin>>a>>b>>c; 	
  x=(b*b+c*c-a*a)/(2*b*c);
  y=(a*a+c*c-b*b)/(2*a*c);
  z=(a*a+b*b-c*c)/(2*a*b);
  if (x&&y&&z<0)
  {
    cout<<"acute";
  }
  else if (x||y||z==0)
  {
    cout<<"rect";
  }
  else if (x||y||z>0)
  {
    cout<<"obtuse";
  }
  else 
  {
    cout<<"impossible";
  }

return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2020-06-17
@dikxarper

1. Acute - when all cosines are less than zero?? vice versa - when everything is greater than zero
2. Impossible - check for a triangle inequality (or the same in essence - that all cosines < 1 modulo)
3. Well, learn to write the conditions:

x&&y&&z<0   ->   x<0 && y<0 &&z<0
x||y||z==0   ->   x==0 || y==0 || z==0

R
res2001, 2020-06-17
@res2001

From a C++ point of view, there are no errors. The program should assemble and work.
But she doesn't seem to get the job done right.
The algorithm for solving the problem is googled in 10 seconds. It is also not difficult to find a condition for the possibility of constructing a triangle on given sides.
Apparently, the conditions in the if you have written incorrectly. From the point of view of the language, they are correct, but they most likely do not what you want. And you can only guess what you want.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question