Need advice/clarification of while loop problem. DUE IN 1 HOUR

Ok so I have homework due in 1 hour. Im not asking you guys to do it for me, but to help me understand the problem and point me in the right direction. So heres the problem:

A geometric series is defined by the following:

a + ar + ar2 + ar3 + ... + arn-1

Using this information write a C++ program that uses a while loop to both display each term and determine the sum of a geometric series having a = 1, r = 0.5, and n = 10. Make sure your program displays the value it has calculated.

The trouble I'm having is I dont really understand what I'm supposed to do.

Thanks for your help =)
Notice how the exponents increase:
ar0+ar1+ar2 etc

and how the result will change:
result = 0
result = result + ar0
result = result + ar1
Thanks that helps a lot with my loop. Now how is the output supposed to look considering the problem? I dont understand that.
The result should probably just be printed with a cout statement, surely? Or are you meant to display exponents as well? There's probably something in the iomanip library to print superscript.

http://www.cprogramming.com/tutorial/iomanip.html
Last edited on
Haha I know that, i meant what does it mean by "display each term and determine the sum of a geometric series"

I dont know what it means by that :P

Im completely at a loss I dont even know how to start a loop with this haha. This is all I have so far:

1
2
3
4
5
6
7
8
9
int main()
{
	int count = 0;
	int a = 1;
	int r = 0.5;
	int n = 10;
	
	
	cout <<

Last edited on
It means to display the numerical value of each term.

The numerical value of the first term is 1.
Of the second term is 0.5 (1 * 0.5^1).
Of the third term is 0.25 (1 * 0.5^2).

etc.

Look at the formula.
The Nth term has value a * r^(N-1).
Or, you can express the value of the Nth term in terms of the (N-1)th term:

T(N) = T(N-1) * r

I'm not sure how to solve this mathematically; I don't even know what a geometric series is; but I'm fairly sure that s/he wants you to std::cout the operands (a, r and n are operands), each operation being undertaken (xy is an operation) and the result.

For example:

1
2
3
for (int i = 0; i < n; ++i) /* or for (int i = 0; i <= (n - 1); ++i) */
    count += pow((a * r), i);
}


I'm fairly sure that's pretty much your loop.

The Nth term has value a * r^(N-1).

OH MAN I HATE THOSE!
Last edited on
Ok so heres my result (output):

1
2
3
4
5
6
7
8
9
10
1.000000
0.500000
0.250000
0.125000
0.062500
0.031250
0.015625
0.007813
0.003906
0.001953
1.998047


and my source:

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
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main()
{
	int a = 1;
	double r = 0.5;
	int n = 1;
	double term = 0;
	double result = 0;
	
	
	while (n <= 10)
	{
		term = a * pow(r,(n-1));
		cout << setw(8) << fixed 
		     << term << endl;
		result = term + result;
		n++;
	}
	cout <<"--------" << endl;
	cout << setw(8) << fixed << result;
	
	return 0;
}


Does that look right?

edit: I wish I could use a for loop =(
Last edited on
It seems ok, line 20 can be shortened to result += term; and thing would get easier if you initialize n with 0 instead of 1

/not related:

Intresting this table:
1
2
3
4
5
6
7
8
9
10
1.000000
0.500000
0.250000
0.125000
0.062500
0.031250
0.015625
0.007813
0.003906
0.001953
1.998047
another weird BBCode thing!
Last edited on
As I say, I can't really tell because I don't know how to do this mathematically.

However, as you don't know how for loops work, I will explain;
1
2
3
for (int i = 0; /* This is asserted to be true (if i != 0, then i will become 0 */
     i < j;     /* This is evaluated every time the loop completes an iteration */
     ++i;)      /* When the second part is evaluated, if it is true; this is done. If it is false, the loop is finished */


1
2
3
4
5
6
int i = 10, j = 15;
for (i = 0;  /* Even though i previously was ten, it is now 0 */
     i < j;  /* If this is not true, the loop continues       */
     ++i;) { /* This is done every time the loop continues    */
    std::cout << i << " ";
}

thus the output will be
1 2 3 4 5 [...] 15


the above for loop is equal to

1
2
3
4
5
6
7
int i = 10, j = 15;

i = 0;
while (i < j) {
    std::cout << i << " ";
    ++i;
}


if you're unsure, ++i is prefix increment; meaning that i has 1 added to it before it is used; e.g.

1
2
3
int i = 1;
if (i++ == 2) std::cout << "i++ == 2\n";
else if (++i == 2) std::cout << "++i == 2\n";

The first is false because i is evaluated for two-ness before it is incremented. The second is true (and therefore executed) because i is incremented and then checked against two.
Last edited on
Thank you all. My program was right.

It would have been a lot easier with a for loop, but the problem stated to use a while loop =(

Topic archived. No new replies allowed.