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.
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?
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
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...
#include <iostream>
#include <vector>
usingnamespace 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!
elseif (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.
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?
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!