#include <iostream>
usingnamespace std;
void print_partitions(int sum_val, int n) {
int pos = 0, last = n - 1;
int a[n]; // dynamic stack-allocated arrays are a gcc extension
for (int i = 1; i <n; i=i+1){
a[i] = 0;
a[0] = sum_val;
}
while (true) {
for (int j=0; j< n;j++)
{
cout<<a[j]<<" " ;
}
cout<<endl;
if (pos != last) {
a[pos]=a[pos]-1;
pos=pos+1;
a[pos] = 1;
}
else {
if (a[last] == sum_val)
return;
for (pos=pos-1; a[pos] == 0; pos--);
a[pos]=a[pos]-1;
int tmp = 1 + a[last];
pos=pos+1;
a[last] = 0;
a[pos] = tmp;
}
}
}
int main() {
print_partitions(3, 3);
return 0;
}
Hi guys I can't understand this for (pos=pos-1; a[pos] == 0; pos--);
First, it is with semicolin then it means a[pos]=a[pos]-1; is not a part the loop?
Second, it starts from pos=pos-1 and ends by what?
And the last questions is a[pos] == 0 a condition to end the loop or the thing in the loop?
Seems All this loop is written by one line for (pos=pos-1; a[pos] == 0; pos--);
Regards, Alex
you can always rewrite a loop.
but you can't get much more speed out of it, most rewites will be worse. what is your goal? Rewriting working code just because you don't like it is folly. If the reason you do not like it can justify the change, that is another story (bug fix, faster, etc).
for (pos=pos-1; a[pos] == 0; pos--);
a[pos]=a[pos]-1;
I think this is the same, and not really any more readable. Ill let you see if its the same or not; remember that when changing things you have to validate it.
do
{
pos--;
if(!a[pos]) a[pos] = -1;
}while(a[pos])
or, just as bad as the original (and is it identical?)
while(a[--pos]);
a[pos] = -1;
@AlexNumbers
People occasionally confuse "visual studio" and "visual basic". Is this a C++ project being written in Visual Studio (the IDE), or is it a Visual Basic (the language) project?
Also, for future reference, saying "it doesn't work" makes it really annoying to help you. Imagine if your compiler said "it doesn't work" when you gave it invalid code, without telling you what's wrong with it.
... if you want to convert that original mess to VB without knowing both languages, you are going to be busy for a few days!
bah ... all mine above were backwards to the original -- this is why you test and why tampering for the sake of tampering is bad!
same result for all 3 loops here with start = 5 and 6. I think its ok now. But it isna VB and it isna any better than the original. All this does is say that you can rewrite any type of loop as any other with some effort.
So I need it to correct in c++ first than after to transfer it to visual basic as it seems it's imposible to write this loop in vb in this matter. That is why I ask your help.
I'm sorry if I told you something wrong. I use online compiler for c++ code.
#include <iostream>
usingnamespace std;
void print_partitions(int sum_val, int n) {
int pos = 0, last = n - 1;
int a[n]; // dynamic stack-allocated arrays are a gcc extension
for (int i = 1; i <n; i=i+1){
a[i] = 0;
a[0] = sum_val;
}
while (true) {
for (int j=0; j< n;j++)
{
cout<<a[j]<<" " ;
}
cout<<endl;
if (pos != last) {
a[pos]=a[pos]-1;
pos=pos+1;
a[pos] = 1;
}
else {
if (a[last] == sum_val)
return;
//for (pos=pos-1; a[pos] == 0; pos--);
while(!a[--pos]);
a[pos] = -1;
a[pos]=a[pos]-1;
////////////////////////
int tmp = 1 + a[last];
pos=pos+1;
a[last] = 0;
a[pos] = tmp;
}
}
}
int main() {
print_partitions(3, 3);
return 0;
}
I'm really cannot understand why the last ones gives the wrong result when it should give right result.
I've check it on this compiler http://cpp.sh/
Please, help me.
Edit 1
Forgot to add a[pos]=a[pos]-1; on two last ones. Even with that it gives wrong results
Edit 2 Thank you keskiverto! Havent seen your post
You learn on your own, like "at home". This task, "work", helps you learn. How is that no "homework"?
You seem to have misunderstood what the word "homework" means, in English. It doesn't just mean "work that you do at home". It specifically means "work that you have been instructed to do in your own time by a teacher, as part of an educational course".
Typically, it's the work that pupils are expected to do at home outside school hours, and then hand in to their teacher when back at school.