Loop Help
...
Last edited on
Yeah because basically you are doing an arithmetic sequence like this:
an = 2 + 2(n-1);
to find the sum of a sequence it will be
sn = n / 2 * ( a1 + an );
so say you have num = 5;
a
1 = 2
a
2 = 4
a
3 = 6
a
4 = 8
a
5 = 10
so if we do it by hand we have 2 + 4 + 6 + 8 + 10 = 30
or we can use the sum formula
s
5 = 5 * ( a
1 + a
n ) / 2 = 5 * ( 2 + 10 ) / 2 = 5 * ( 12 ) / 2 = 60 / 2 = 30
so you could simply do this for your display_totals
1 2 3 4
|
void doubleup::display_totals()
{
std::cout << "Total: " << num * ( 2 + 2*num) / 2 << std::endl;
}
|
Topic archived. No new replies allowed.