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;

a1 = 2
a2 = 4
a3 = 6
a4 = 8
a5 = 10

so if we do it by hand we have 2 + 4 + 6 + 8 + 10 = 30

or we can use the sum formula
s5 = 5 * ( a1 + an ) / 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.