Hi I am new to C++ and have an assignment asking me to write a program to approximate the sum of a given alternating series so that the error in the sum is smaller than a given tolerance. The program should request the user to enter a non-negative error tolerance, and then add terms in order until the error between the sum of the series and the partial sum is less than the
tolerance. You can use the error bound from the alternating series test to determine when to stop adding terms. Report the sum of the series and the number of terms computed to the user.
The alternating series is: the sum from k=0 to infinity of [(-1)^k]/(3k+1)
The advice I have gotten is to declare two variables, "curr_sum" and "past_sum". In every iteration, you add a term on "curr_sum", and check the difference between "curr_sum" and "past_sum". If the difference is less than the tolerance, you stop the iteration, and "curr_sum" is your approximation. If the difference is still too big, set "past_sum = curr_sum" and continue the loop until the difference is very very small (less than the tolerance). It is from the idea of convergent infinite series in Calculus. However, I am still very confused how to execute all of that after declaring variables.
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.
We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.
What part are you confused about? Break the problem down into simpler parts.
A series where you go from k to some indefinite number is the equivalent of a while loop in C++.
int k = 0;
double curr_sum = 0.0;
double past_sum = 0.0;
double threshold = /* some small value, like 0.00001 */;
while (true) // indefinite loop
{
value = /* that math function you have, translated into C++ */;
/* set curr_sum to be value plus past_sum */
/* set past_sum to be curr_sum */
if (difference between past sum and current sum < threshold)
break; // "breaks" you out of the loop immediately.
/*increment k by 1 */;
}
(-1)^k in C++ is written as std::pow(-1, k). You'll need to #include <cmath> at the top of your file.
Make sure that your sum comes to the same number as (ln 2)/3 + pi / ( 3.sqrt(3) )
Remember that the difference between current_sum and past_sum ... is simply the term that you've just added. (Not that it's what is asked, but given the form of that term, you could work out precisely how many terms are needed to meet any given tolerance.)
True, that's definitely less intensive, I just went for the most literal translation as possible, since the ^ operator is often confused to be exponentiation and not XOR.
PS edited my first post with a few more hints, since you did at least explain your problem a bit more than average person.