Home c++ Segment division method in half

Segment division method in half

Author

Date

Category

In a properly written program, the number of calculation features differs from the number of iterations of the method on 2. How to fix the program to work correctly?

# include & lt; stdio.h & gt;
#Include & lt; math.h & gt;
INT SIGN (DOUBLE X);
Double SHAQ (Double X);
Double My_Exp (Double X);
INT ROOT_HALF (Double A, Double B, Double (* F) (Double), Double EPS)
{
  Double C; int chislo = 0, funci = 0;
  if (Sign (F (A)) == SIGN (F (B))) {PrintF ("method is not applicable \ n"); Return 0;}
  do.
  {
    C = (A + B) /2.0;
    if (SIGN (F (C)) == 0) {FUNCI ++; Printf ("Root:% LF \ N", C); break;}
    ELSE.
    IF (Sign (F (a)) == Sign (F (C))) {PrintF ("root:% lf \ n", c); a = c; funci + = 2;}
    ELSE.
    {printf ("root:% lf \ n", c); b = c;}
    Chislo ++;
    }
while (fabs (b-a) & gt; = eps);
Printf ("Number of iterations:% d \ n The number of calculations of functions:% d \ n", Chislo, FUNCI);
Return 0;
}
INT SIGN (DOUBLE X) {
  IF (X & GT; 0) Return 1;
  ELSE.
  IF (X & LT; 0) Return -1;
  ELSE if (x == 0) Return 0;
  Return 0;
}
INT MAIN (Void)
{
  Double A, B, F (Double), EPS = 0.01;
  Scanf ("% le% lf", & amp; a, & amp; b);
  root_half (a, b, & amp; f, eps);
  Return 0;
}
Double F (Double X)
{
  Return x * x * x -2 * x * x + 3;
}

Answer 1, Authority 100%

I would do this:

# include & lt; stdio.h & gt;
#Include & lt; math.h & gt;
INT FUNCI;
Double F (Double X)
{
  ++ FUNCI;
  Return x * x * x -2 * x * x + 3;
}
Double Root_half (Double A, Double B, Double (* F) (Double), Double EPS)
{
  Double C; int chislo = 1;
  double Fa = F (a), fb = f (b);
  if (FA * FB & GT; 0) {PrintF ("method is not applicable \ n"); Return 0; }
  for (; Fabs (B-a) & gt; = eps; chislo ++)
  {
    C = (A + B) /2.0;
    Double FC = F (C);
    if (fc == 0)
    {
      PrintF ("Number of iterations:% d \ n", chislo);
      Return C;
    }
    if (FC * FA & GT; 0) {a = c; } else {b = c; }
  }
  PrintF ("Number of iterations:% d \ n", chislo);
  Return C;
}
INT MAIN (Void)
{
  Double A, B;
  Scanf ("% LF% LF", & amp; a, & amp; b);
  printf ("root =% lf \ n", root_half (a, b, f, 1e-5));
  PrintF ("Function calls:% d \ n", Funci);
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions