Need help with consecutive numbers

Okay so I've been practicing the for loop lately and I found this program that I just can't seem to understand. Here it is:
For an entered number, find all consecutive addends of that number and print them.
For example, If I enter 9 the output should be:
2,3,4
if i enter 15:
1,2,3,4,5
4,5,6

I've tried everything, also I'm not supposed to use arrays, at least I think so, the array chapter is a bit later in the practice book I'm using.
So far, I understand that I need two for loops, and the first one should be
for(int i=1;i<=n/2;i++)...at least i think so. Makes no sense to go further than half the number entered.
Any help would be appreciated.

You will not need arrays to do this. Let's think about how the logic of this program could go:

Step 1: Get input value (in example this is 9)

Step 2: Consider all the positive integers smaller than the input number. This means a for-loop. (or, if you want to think mathematically, do you REALLY need to consider ALL the numbers smaller than the input number ;) )

Step 3: For each integer considered, add upwards consecutively until the total is no longer less than the input value (while-loop).

Step 4: See if the numbers added up to exactly the input number. If this is the case, then out put the numbers with another for-loop.

Hope this is clear enough!
Let me use a differently named variable ...
for ( int start=1; start <= n/2; ++start )

Now, you want to compute a sum. The first term fo the sum equation is 'start'.
While you are adding terms to the sum, there are three possible states:
a) the sum is still less than n
b) the sum equals n
c) the sum exceeds n

It is possible to break; out from a loop.


PS. 4+5==9 and 7+8==15 too. Do the sums have to have more than two terms?
thank you both for your help, but i'm still kinda stuck. let me instead ask if this can somehow work:

1
2
3
for(int i=1;i<=n/2;i++){
    for(int j=i;j<=n/2;j++)
       .....}

the idea is that this would use every number before the entered number as a starting point for the sum, and then it would find all the possible solutions, also yes, every solution needs to be there, i forgot those two lol.
but i'm not sure how to "extract" the numbers from this
Last edited on
PrivateRyan mentioned not two, but three loops.
so I'm guessing that the third for loop would be used to print the numbers. i'll try again tomorrow morning, it's getting kinda late here and i'm tired, thank you both of you
I was thinking of three loops, but the second and third loops are both nested in the first loop. There might be a more elegant way to do it than what came up in my head.
It's quite possibly not the most elegant solution, and to be honest, I don't have much interest in beautifying it, or even testing it in great detail, but...

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
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
using namespace std;

vector<int> compute_addends(int n);

int main()
{
       //need sequential addends to equal a number n
	cout << "Enter a number: ";
	int n;
	cin >> n;
	
	vector<int>addends = compute_addends(n);

	for (int x : addends) cout << x << ' ';
	cout << endl;

       return 0;
}

vector<int> compute_addends(int n)
{
	vector<int>store_addends;
	for (size_t i = 1; i != n; ++i) {
		unsigned sum{ i }; //Does what it says.
		unsigned add{ i }; //We'll be adding one to this and storing it for sequencing
		store_addends.push_back(add); //Add the first element
		while (sum < n) {
			++add; //Sequencing.
			sum += add; //Tallying.
			if(sum <= n) store_addends.push_back(add); //If we need to go further.
			if (sum == n) return store_addends; //HUZZAH! We did it!
			else if (sum > n) store_addends.clear(); //Didn't work? Clear, back to for()
		}
	}
	store_addends.clear(); //Can't think of better way if sequence not found
	return store_addends; //Only get here if sequence not found
}


EDIT: Some VERY minor formatting (I'm not about to fix indenting) and adding comments for clarity.
Last edited on
Yawzheek:

I believe OP was looking for a solution that did not involve data structures.
I believe OP was looking for a solution that did not involve data structures.


Ah, I see. I guess I got a little carried away when I saw a problem I wanted to help solve, and didn't notice it was one of those, "let's complicate your life a bit by adding complexity through arbitrary limitations" deals :^)

EDIT: I guess you could do the same thing I outlined above, but store the values in a string... ? Perhaps?
Last edited on
closed account (48T7M4Gy)
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
28
29
30
#include <iostream>

int main()
{
    int number = 0, total = 0, limit = 0;
    
    std::cout << "Please enter an integer: ";
    std::cin >> number;
    
    limit = number / 2 + 2;
    
    for(int i = 1; i < limit; i++)
    {
        total = 0;
        
        for (int j = i; j < limit; j++)
        {
            total += j;
            
            if (total == number)
            {
                for (int k = ???) // addends
                    std::cout << ??? << ' ';
                
                std::cout << " Total: " << total << '\n';
            }
        }
    }
    return 0;
}


Please enter an integer: 873
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57  Total: 873
93 94 95 96 97 98 99 100 101  Total: 873
143 144 145 146 147 148  Total: 873
290 291 292  Total: 873
436 437  Total: 873
Program ended with exit code: 0


Please enter an integer: 15
1 2 3 4 5  Total: 15
4 5 6  Total: 15
7 8  Total: 15
Program ended with exit code: 0
Last edited on
thank you, kemort, the third for loop goes from i to j, and prints out k. i've also decided to remove the limit variable and it still works, thank you!
What do you use in place of limit?

This is something a bit different:
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
28
29
30
31
32
33
34
35
#include <iostream>

int main()
{
    int number = 0, total = 0, limit = 0;
    
    number = 15; // fake input
    
    limit = number;
//    limit = number / 2 + 2;
    
    for(int i = 1; i < limit; i++)
    {
        std::cout << "i=" << i << " : ";
        total = 0;
        
        for (int j = i; j < limit; j++)
        {
            total += j;
//            if ( number < total )
//            {
//                break;
//            }
            std::cout << j << ' ';
            
            if (total == number)
            {
                std::cout << " Total: " << total << ' ';
            }
            
        }
        std::cout << '\n';
    }
    return 0;
}

It does lack the "show addends", for that you can already do.

1. Run it as is.
2. Run it with the lines 20-23 uncommented.
3. Run it with line 10 uncommented.
Topic archived. No new replies allowed.