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.
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.
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);
}
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";
elseif (++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.