i dont understand this question.

I totally don't understand this question. Can help me out?
Question 5.13.
What the question wants?
The question is in the image below.
http://imageshack.us/a/img845/6828/question513.jpg

Please help me =(
You are required to calculate the sum of a series.
Well, the series has an infinite number of terms, so you can't actually do the entire series. So just accumulate the total of the first n terms.

This is known as Gregory's series. If you google that, perhaps in relation to finding an approximation to PI, you should find all sorts of useful stuff.
Last edited on
After doing the total of the first n terms, the part of the (....-1/ 2i-1 + 1/2i+1)
how does this part comes about? i dont quite get it. can u show a short example?
http://www.craig-wood.com/nick/articles/pi-gregorys-series/

The part at the end is just a general representation for finding the nth term in the series. Because the sign alternates from plus to minus, two successive terms are given.
(well, I say nth, in the original question it is i'th).
Last edited on
that means 'i' is a input from user right?
that means 'i' is a input from user right?


i must be given in some form or another.
that means 'i' is not a input? then where we get 'i'? i mean how we know there are how many terms? and whats the m(i) for? the question is so short without any extra information =x
Think of it as a function like sin(i) or sqrt(i) or log(i). Don't worry about the value of i. That's the easy part.

All you have to do is write the function, which will work for any value of i.
 1st term is +1 / 1
 2nd term is -1 / 3
 3rd term is +1 / 5
 4th term is -1 / 7
 5th term is +1 / 9
 6th term is -1 / 11
 7th term is +1 / 13
 8th term is -1 / 15
 9th term is +1 / 17
10th term is -1 / 19


so for example m(5) = 1 - 1/3 + 1/5 - 1/7 + 1/9

If you can understand this, then that's all you need to know.
Last edited on
okay thank you i will try it out =)
This is my code so far.
but i get the output 4 no matter what number i cin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;


double a (int b)
{
double c=0;
double term;
int sign = 1;
for ( int i =1; i<=b; i++)
{term = sign * 4* (1/(2*i-1));
c+=term;
sign = -1 * sign;

return c;}
}

int main ()
{
	int z;
	cout << "Enter the number of terms you want the formula to calculate: ";
	cin >> z;
	cout << "The answer is " << a(z) ;


   return 0;
}
That's a good start. Now it needs de-bugging.

Depending on your compiler, you may get a warning message. Mine issues a warning about function a, "Function should return a value".

Anyway, let's take a closer look. If we use indentation and spacing to improve legibility, the problem may be come clearer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
double a (int b)
{
    double c = 0;
    double term;
    int sign = 1;
    for ( int i =1; i<=b; i++)
    {
        term = sign * 4* (1/(2*i-1));
        c += term;
        sign = -1 * sign;
        
        return c;
    }
}

Notice line 12, return c; is inside the for loop. That means the first time around the loop, the value is returned, thus terminating the loop and the function as a whole. Move the return after line 13, outside the loop.

There's another problem, which you won't have noticed so far, as the above problem was hiding it.

If you temporarily insert this line, purely to see what is going on, inside the for loop (after line 10), it will show what is happening:
 
        cout << "  i: " << i  << "  term: " << term << endl;
... try it and see.

The problem here is all the values in the calculation of "term" are integers. That seems reasonable at first glance. But that means integer division will be used. The result of an integer divide is always a whole number, there cannot be any decimal places, the value is simply truncated.

A simple answer to that is to use values such as 4.0 and 2.0 instead of 4 and 2. That will force the compiler to use floating-point values rather than integers, and consequently it will use a floating-point divide operation.
Last edited on
Topic archived. No new replies allowed.