The school has just opened the programming lesson, because I am studying software development, of course, I am very interested in this thing. Since I am a big new life, the level is limited, it is not very familiar with C, in order to achieve my own requirements, here is here to share the problems I have to solve with everyone. I hope to learn something from Middle School with my same level.
Below is the problem I have encountered in actual operations:
What is said how to use C to solve E's approximation: E = 1 1/1! 1/2! 1/3! ... 1 / n! This is the Taylor expansion of E.
The solution I started was as follows:
#include "stdio.h" void main () {INT N, S; FLOAT E = 1.0, T, K = 0; Printf ("Show me the N:"); scanf ("% d", & n); for (s = 1; s <= n; s ) {t = 1.0 / s; k = t / s; E = T;} Printf ("AHA Anwser IS:% F / N", E);}
Result error occurred: 0 Error 1 WARNING
That is, it can be passed but incomplete, because this has nothing to do, so I run directly.
The result is 3.928968 and actual results.
Later I went back to check and found the problem in the For loop.
{T = 1.0 / s; k = t / s; E = T;}
S = 1 can be understood. When S = 2, k = 1.0 / s * s is clear that this is not what we want. It belongs to the logic unclear.
I will find it next, I am still very simple for my own trouble, I haven't seen it, I just want to see it.
{T = t / s; // either this t / = s; the result is the same. E = T;}
Because I do this, the value of the variable T can be used again, the last T value is given to the new T to T = 1/1 * 2 * 3 ... * n = 1 / n!
This is what I want, K's variable can be left.
#include "stdio.h" void main () {int N, s; float e = 1.0, t = 1.0; Printf ("Show me the n:"); scanf ("% d", & n); for (S = 1; s <= n; s ) {T = T / S; E = T;} Printf ("AHA Anwser IS:% F / N", E);
}
Such a result is correct, E = 2.718282.
Through the explanation of logic, you can see the drain of the program, which is not detected by compilation, which requires your long-term experience and logical identification.