Being T
one of the terms of the series in question, given in general by:
T = SINAL * (X ^ EXPOENTE)
--------------
DIVIDOR!
And whereas:
SINAL
is a number that alternates between values 1
or -1
, beginning with -1
EXPOENTE
is a sequence that begins with 2
and increasing of 2
in 2
until the operation is completed (2, 4, 6, ..., N
)
DIVISOR
is a sequence that begins with 3
and increasing of 2
in 2
until the operation is completed (3, 5, 7, ..., N
)
And yet:
S
is the value of the current sum
and its initial value is X
S_ANTERIOR
is the value of the previous iteration
X
is the value entered by the user
Do the following:
- Declare variables to control the above values
- Initialize variables with the initial value of each of them
- Make a loop that:
- Calculate the value of
T
as in the above function
- Save the value of
S
in S_ANTERIOR
- S_ANTERIOR = S
- Some
S
with T
- S = S + T
- Check the accuracy of the result.
This can be done by checking whether S - S_ANTERIOR < 0.000001
, because it means that all other calculated terms will not affect the first six decimal places of the sum.
- If the condition is true, end the loop.
- If the condition is false, calculate the next values of
SINAL
, EXPOENTE
and DIVISOR
and return to the beginning of the loop (item 3.1)
The hardest thing for those who are starting programming is not programming, but understanding the problem first.
What I did above was describe the implementation logic in terms of an algorithm.
Whenever you have a problem at hand, the first thing you need to understand is how the problem is solved in algorithm format, which is nothing more than a set of steps to get to the result in general deterministic or iterative way.
Once you understand how the problem is solved, here comes the part of translating this into a program.
You will probably encounter specific difficulties in the course of implementation, but once you know what you want, it is easy to find the answer.
How to receive the initial value, how to declare the variables and perform the mathematical operations, all these are questions that you can easily now find the answer, after all know exactly which question to ask.
Input Examples: 2 5 --> output: 1.454674
– João Carlos
What have you tried so far? Factorials are a very simple concept for the program. You really need to break this question into functions.
– CBredlow