R
R
replechaun2021-11-23 01:46:14
C++ / C#
replechaun, 2021-11-23 01:46:14

What to do if the program gives an error when entering the double function?

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
//Функция для полинома:
double F(double x) {
  return 5 * tan(x) - (10 / (x) ^ 2) - 3;
}
//Функция поиска корня:
double FindRoot(double (*f)(double), double a, double b, double eps) {
  double c;
  while ((b - a) / 2 > eps) {
    c = (a + b) / 2;
    if ((f(a) * f(c)) > 0) a = c;
    else b = c;
  }
  return c;
}
int main() {
  //Интервал, погрешность и корень:
  double a, b, eps, x;
  cout << "interval: ";
  cin >> a;
  cin >> b;
  //Проверка корректности интервала:
  if (F(a) * F(b) > 0) {
    cout << "Wrong interval!\n";
    return 0;
  }
  cout << "error: ";
  cin >> eps;
  //Поиск решения:
  x = FindRoot(F, a, b, eps);
  cout << "x = " << x << endl;
  return 0;
}


when you enter this program, you get error E2140 "expression must be an integer or enumeration type with no scope"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-11-23
@galaxy

It would be nice to write in which line the error is (and for starters, format the code normally - there is a button in the editor).
Apparently, the problem here return 5 * tan(x) - (10 / (x) ^ 2) - 3;
(x) ^ 2is not exponentiation, but XOR (exclusive OR). Squaring like this: x*xfor example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question