naustin288
naustin288

Reputation: 13

Whats wrong with my program on how to calculate the series C++? Been stuck for weeks

#include <iostream>
using namespace std;

int main () {
    int input {};
    int sum {0};
    int tsum {0};

    cout << "Number: ";
    cin >> input;

    for (int i {1}; i <= input; i++) {
        for (int j {1}; j <= input; j++) {
            sum += j;
            tsum += j;
            cout << j;
            if (j < i) {
                cout << "+";
            }
        }
        cout << " = " << tsum << endl;
    }
    cout << "Your sum is: " << sum << endl;
}

So I found this problem on w3resources for Loop exercises and It wants me to calculate the series so for Example: Lets say my input is 2

it would be output 1 = 1

1 + 2 = 3

The sum of the series is 4

I have been looking at this problem for a week now but cannot wrap my head around it and I even looked at the solution and still do not understand it. Can someone please try to explain it to me because it is very frustrating. I do understand that we need a Nested Loop to get the two numbers to add up but how does sum+=j; and tsum+=j fit in this equation? wouldn't it just be 1+1 = 2 , 1 + 2 = 3 and then 2 + 1 = 3 , and 2 + 2 = 4? How would that get me to my solution????? and how you also fit " = " into play and " + "? and how wouldn't sum and tsum be the same number because they are both adding j to its value?

Upvotes: 0

Views: 108

Answers (1)

WhozCraig
WhozCraig

Reputation: 66194

First, you didn't enter the proposed solution code properly. It is presented here. See how it differs from what you posted (which I have marked with // *** HERE ***:

#include <iostream>
using namespace std;

int main()
{
    int i, j, n, sum = 0, tsum;
    cout << "\n\n Find the sum of the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n):\n";
    cout << "------------------------------------------------------------------------------------------\n";
    cout << " Input the value for nth term: ";
    cin >> n;
    for (i = 1; i <= n; i++) 
    {
        tsum = 0;
        for (j = 1; j <= i; j++) //*** HERE ***
        {
            sum += j;
            tsum += j;
            cout << j;
            if (j < i) 
            {
                cout << "+";
            }
        }
        cout << " = " << tsum << endl;
    }
    cout << " The sum of the above series is: " << sum << endl;
}

Fix that and the exercise makes more sense. That said, the goal of your exercise is to calculate a sum of sums. Given some number natural number n, the algorithm is simply:

sum = 0;
for (i = 1 through n)
    for (j = 1 through i)
        sum = sum + j;

Suppose the input number was 3. That means you're calculating

(1) + (1+2) + (1+2+3)

Alternative #1 : Regrouping

Note that there are a number of of optimizations you can make to this, based on the recognition of patterns in the sequence above. For example. If you rearrange the number like so:

(1+1+1) + (2+2) + (3)

you can see there are n 1's, (n-1) 2's, etc. up to 1 3's. This pattern holds true for whatever n you deliver. Therefore, you can do this instead:

sum = 0
for (i = 1 through n)
    sum = sum + (n - (i-1))*i;

Alternative #2 : Known Sums

A closed form exists for the sum-of-natural-numbers over {1..n}:

sum{1..n} = n*(n+1)/2

Therefore, we can eliminate the inner loop, and simply do this:

sum = 0
for (i = 1 through n)
    sum = sum + (i * (i+1))/2;

This, the previous solution, or the two-loop solution, will all deliver the same result. Which you choose is up to you.

Upvotes: 2

Related Questions