Level 2 C language (2)

xiaoxiao2021-03-05  19

Example 3. Please prepare a functionality of FUN (), which is:

S = 1 / (1 × 2) 1 / (2 × 3) ... 1 / (N × (n 1)))

For example: if n = 10, the S value is 0.909091.

Question procedure: (Similar Example 2)

#include

#include

#include

Double Fun (int N)

{

}

Main ()

{

Int n;

Double S;

CLRSCR ();

Printf ("/ N / NINPUT N:");

Scanf ("% d", & n);

S = Fun (n);

Printf ("/ N / NTHE S =% f / n / n", s);

}

Reference answer: (Note there is an error)

Double Fun (int N)

{

INT I;

Double S = 0.0, T = 0.0;

For (i = 1; i <= n; i )

{

/ *** there is a error in here *** /

T = 1 / (i * (i 1));

/ *********** * End ************** /

S = T;

}

Return S;

}

Example 4. Please prepare a functionality of FUN (), which is:

S = 1 x x2 / 2! x3 / 3! ... XN / N!

For example: if n = 10; x = 0.3, the S value is 1.349859.

Question procedure:

#include

#include

#include

Double Fun (Double X, INT N)

{

}

Main ()

{

Int n;

Double X, S;

CLRSCR ();

Printf ("/ N / NINPUT N:");

Scanf ("% d", & n);

Printf ("/ n / ninput x:");

Scanf ("% lf", & x);

s = fun (x, n);

Printf ("/ N / NTHE S =% f / n / n", s);

}

Reference answer:

Double Fun (Double X, INT N)

{

INT I, J, T = 1;

Double S = 0.0, P = 0.0;

For (i = 1; i <= n; i )

{

T = 1; for (j = 1; j <= i; j ) {t = T * j;} / * equivalent to t * = i; * /

P = p * t;

S = S P / T;

}

Return S;

}

There is also a remember X0.5 = √X (the square of X).

转载请注明原文地址:https://www.9cbs.com/read-38746.html

New Post(0)