Answer the question
In order to leave comments, you need to log in
How to find the value of an expression?
Given:
Here is my code:
//найти значение выражения
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "RUS");
float X;
cout << "x = ";
cin >> X;
if ((X - 5) / (X * X - 9) < 0) cout << "Корень из отрицательного числа\n";
else if (X != -1) cout << "Логарифм отрицательного числа\n";
else if (X * X == 9) cout << "Деление на ноль\n";
else
{
float Y = (sqrt(X - 5 / X * X - 9)) + log((X * X + 2 * X + 1));
cout << "F = " << Y << endl;
}
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
Your order of checks is wrong.
First you need to check that the denominator is not zero, and only then that the whole fraction is non-negative.
To check that the logarithm is not taken from a negative number, you need to check exactly this (that the expression under the logarithm >=0). For some reason you check that it is equal to zero.
Also, your expression is incorrect. You need to put the brackets. (sqrt(X - 5 / X * X - 9) will calculate the root of x minus 5 divided by x^x, minus 9 - 3 terms, of which only the second is a fraction. In
total, you need to:
- rearrange the conditions so that the division by 0 was the first
- correct the condition for the logarithm of a negative number
- place brackets in the expression.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question