C
C
CodeInside2015-10-11 19:55:10
C++ / C#
CodeInside, 2015-10-11 19:55:10

Where is the logical fallacy?

Task: find the area of ​​an isosceles trapezoid with bases a and b and angle alpha with a larger base a.

int a,b;
        cout << "Input length of the lower base: ";
  cin >> a;
  cout << "Input length of the upper base: ";
  cin >> b;

  float angle;
        cout << "Input angle: ";
  cin >> angle;

  angle = (angle*pi)/180.;//перевод градусов у радианы
  float c = (a-b)/2*cos(angle);//сторона трапеции
        //здесь делал точку останова - данные правильно вычисляются
  float s = ((a+b)/2.)*sqrt((float)pow(c,2) - ((float)pow(a-b,2))/4.);//вычисление площади http://www-formula.ru/index.php/2011-09-19-02-39-24/trapeze-area

  printf("Area: %.3f\n\n",s);

For example, I enter the following parameters:
a - 20, b - 10, angle - 30. Result: -1.#io. Where is the mistake?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
Rsa97, 2015-10-11
@CodeInside

Too hard to calculate. It is better to first analytically simplify the formula, then there will be less room for errors in the program.
Let the lower (greater) base a , upper (smaller) - b .
The height of the trapezoid (isosceles) h = (ab)*tg(α)/2
The area of ​​the trapezoid S = (a+b)*h/2 = (a+b)*(ab)*tg(α)/4 = (a 2 -b 2 )*tg(α)/4

G
GavriKos, 2015-10-11
@GavriKos

You get a negative number under the root, due to the fact that the lateral side was incorrectly calculated. See right triangle formulas. To make it easier - do not write the whole formula, break it down into its component parts. And debug will be simple.

D
Dmitry Kovalsky, 2015-10-11
@dmitryKovalskiy

Trapezium area: S \u003d (a + b) * h / 2. Where do you get the power functions and the square root I can not understand.
h can be calculated in terms of solving the triangle. where h is one leg, the second: (ba) / 2 since you have an isosceles trapezoid, and the tangent / cotangent of the angle you have.

S
Stanislav Makarov, 2015-10-11
@Nipheris

> (ab)/2
Check if you have an integer division here

V
Vladimir Martyanov, 2015-10-11
@vilgeforce

The debugger must be used, the debugger...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question