Why is it giving an infinite loop?

-1
  

Exercise: Write an algorithm to generate and write a table with   s values of an angle A in radians, using the series of   Mac-Laurin truncated, presented below:

     

A3
  A5
  A7
  sin A = A-6 + 120-5040

     

Conditions: Angle A values should range from 0.0 to 6.3, inclusive, from 0.1 to 0.1.

My code:

#include <stdio.h>
#include <math.h>

int main () {

    float A = 0.0, valor_seno;
    float aprs_tela;

    while (A <= 6.3) {
        aprs_tela = A;
        A = A - (((pow(A,3) / 6) + (pow(A,5) / 120)) - (pow(A,7) / 5040));
        valor_seno = sin(A);

        printf("O valor do seno (%.1f) com a série de Mac-Laurin é %.2f\n\n",aprs_tela ,valor_seno);

        A = A + 0.1;
    }

    return 0;
}
    
asked by anonymous 14.04.2017 / 02:51

1 answer

1

At the beginning of the program you put A = 0.0 ; (A will receive 0).
Then you check whether A is < that 6,3 (yes, for the first loop this is true).

Then you run the line:

A = A - (((pow(A,3) / 6) + (pow(A,5) / 120)) - (pow(A,7) / 5040));

It turns out that A is equal to zero, and zero raised to any number is zero. In summary, (((pow(A,3) / 6) + (pow(A,5) / 120)) - (pow(A,7) / 5040)) this will all give zero.
Then you try to assign A = A - [expressao]; (ie A (which is zero) will get the result of the expression, which is also zero.

Then you put:

A = A + 0.1;

blz here you assign a positive value to "A".
However, a value less than a larger value will always be smaller, for example: 0 - 0,1 = -0,1 (That is, the value of A will always be less than 6.3 so it stays in an infinite loop.)

Can you understand?

    
14.04.2017 / 04:26