Home c The calculation of the sinus using the Taylor series

The calculation of the sinus using the Taylor series

Author

Date

Category

In this task, you need to calculate the sine of the argument, with the use of sin is forbidden, that is, use a Taylor series.
signature:
one integer N is written in the first row – number of arguments for which to calculate the sine (1 & lt; = N & lt; = 10 ^ 5). Next is the N lines, one real number X in each. Each number – a number that is necessary to calculate sine. All of X absolute value does not exceed one (given in radians). It is necessary to bring N rows, each row one real number which is equal to sin X to the corresponding argument of the entered data. It is recommended to display the numbers in the “% 0.15lf” format to output 15 digits after the decimal point. Error in each answer should not exceed 10 ^ 12.
Here is my program, but after testing the system it does not pass, how would it not be changed. Tell me what the problem is, please, and how to fix it:)

# include & lt; stdio.h & gt;
INT MAIN () {
  int n;
  double k;
  double s;
  int n;
  n = 1;
  k = x;
  s = 0;
  scanf ( "% d", & amp; N);
  for (int i = 0; i & lt; N; i ++) {
    scanf ( "% d \ n", & amp; k);
    while (fabs (k) & gt; eps) {
     s + = k;
     N ++;
     k * = - x * x / (2. * n-1.0) / (2.0 * n-2.0);
     printf ( "% 0.15lf", s);
    }
  }
}

upd: here is a new version of my program, but now it simply zeroes out, and every argument of N times …

# include & lt; stdio.h & gt;
# INCLUDE & LT; Math.h & gt;
#define eps 0.000001
double Sin (double x) {
  double d, s;
  int n;
  s = d = x;
  n = 1;
  while (fabs (d) & gt; eps) {
    d * = n * n;
    d * = x * x;
    + N;
    d / = n;
    ++ n;
    d / = n;
    s + = d;
  }
  Return S;
}
INT MAIN () {
  int n;
  double x, h;
  scanf ( "% d", & amp; N);
  for (int i = 0; i & lt; N; i ++) {
     scanf ( "% d \ n", & amp; x);
     printf ( "% 0.15lf \ n", Sin (x));
    }
}

upd 2.0: sincerely trying, but my program does not understand 🙂 Now I do not converge at the types in the body and function. Anyway instinct tells you that a system such a program would want to take. At least somewhere close to the truth?

# include & lt; stdio.h & gt;
# INCLUDE & LT; Math.h & gt;
#define eps 0.000000000001
double Sin (double x, N) {
  double d, s;
  int n;
  s = d = x;
  if (fabs (d) & gt; eps) {
     for (n = 1; n & lt; = N; n ++) {
       s + = d;
       d * = (-1) * x * x / (2 * n) / (2 * n + 1);
     }
  }
  Return S;
}
INT MAIN () {
  int n;
  double x;
  scanf ( "% d", & amp; N);
  for (int i = 0; i & lt; N; i ++) {
     scanf ( "% d \ n", & amp; x);
     printf ( "% 0.15lf \ n", Sin (x, N));
    }
}

upd 3.0: another update 🙂 Again, not very good. All error and warning messages have disappeared, but now the wrong answer to every argument entered. Although the formula is correct. Maybe some error in the cycle?

# include & lt; stdio.h & gt;
# INCLUDE & LT; Math.h & gt;
#define eps 0.000000000001
double Sin (double x) {
  double d, s;
  int n, N;
  scanf ( "% d", & amp; N);
  s = d = x;
  if (fabs (d) & gt; eps) {
     for (n = 1; n & lt; = N; n ++) {
       s + = d;
       d * = (-1) * x * x / (2 * n) / (n 2 + 1 *);
     }
  }
  Return S;
}
INT MAIN () {
  int n;
  double x;
  for (int i = 0; i & lt; N; i ++) {
     scanf ( "% lf \ n", & amp; x);
     printf ( "% 0.15lf \ n", Sin (x));
    }
}

Answer 1, Authority 100%

# include & lt; stdio.h & gt;
# INCLUDE & LT; Math.h & gt;
#define eps 1e-12
double Sin (double x)
{
  double s = x, d = x;
  for (int n = 3; fabs (d) & gt; eps; n + = 2)
    s + = d * = -x * x / n / (n-1);
  Return S;
}
INT MAIN () {
  int n;
  scanf ( "% d", & amp; N);
  double x;
  for (int i = 0; i & lt; N; i ++)
  {
    scanf ( "% lf", & amp; x);
    printf ( "% 0.15lf \ n", Sin (x));
  }
}

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