# write a program that determines all the possible ways to divide an integer.

• 03-17-2010
mayoussa89
write a program that determines all the possible ways to divide an integer.
my program that im supposed to write needs to divide 200 hrs of work between four workers , based on the following conditions:
if worker one works 2 days, then he must work for only 2 hrs each day, totatl hrs=4
if worker two works 4 days, then he must work for only 4 hrs each day, totatl hrs=16
if worker three works 6 days, then he must work for only 6 hrs each day, totatl hrs=36
if worker four works 12 days, then he must work for only 12 hrs each day, totatl hrs=144
total hours= 144+36+16+4=200 hours
therefore, if we divide 200 hours of work over four workers, their number of days worked squared equals the total hours they should work.

my question is, what would be the best way to write this program? using a 'for' loop or a do...while loop?
ive done it both ways, both not working.
can someone please take a look at it?
thanks!
Code:

```#include <iostream> #include <cmath> using namespace std; int main () { int a=0, b=0, c=0, d=0; //int counter; //counter= 1; //a==200-b-c-d && b==200-a-c-d && c==200-a-b-d && d==200-a-b-c do { 50= (a+b+c+d); //a=200-b-c-d; //b=200-a-c-d; //c=200-a-b-d; //d=200-a-b-c; if (sqrt(a)==static_cast<int>(sqrt(a))) cout<<a<<" "; if (sqrt(b)==static_cast<int>(sqrt(b))) cout<<b<<" "; if (sqrt(c)==static_cast<int>(sqrt(c))) cout<<c<<" "; if (sqrt(d)==static_cast<int>(sqrt(d))) cout<<d<<" "; //counter++; cout<<endl; } while (200/4== a+b+c+d); //a+b+c+d==200); return 0; }```

the comments in this program are what i was originally using but made them comments to test if the program would work without them...
and using 'for' loop:

Code:

```#include <iostream> #include <cmath> using namespace std; int main () {         int Employe=0;         int total_hour=0;         for(int i=1;i<=4;i++)         {                 Employe=i;                 if(Employe==1)                 {                         total_hour+=((i+i)*(i+i));                         //cout<<total_hour;                 }                 else if(Employe==2)                 {                         total_hour+=((i+i)*(i+i));                 }                 else if(Employe==3)                 {                         total_hour+=((i+i)*(i+i));                 }                 else if(Employe==4)                 {                         total_hour+=((i*3)*(i*3));                 }         }         cout<<total_hour<<" "<<endl;         return 0; }```
• 03-17-2010
tabstop
What do you intend
Code:

`50= (a+b+c+d);`
to do?

I think the issue here is that you need to generate all the possible quadruples (a,b,c,d) yourself; this is not APL or ... whatever those kind of languages are called ... where you can specify the equations and the computer works out what the solutions are.
• 03-17-2010
mayoussa89
how would i specify the quadruples (a,b,c,d)?
the problem requires me to have the program generate them on its own...
how can i tell the program what conditions i want to be met and when to finish? i assume that it will be some kind of loop that i would have to use...
• 03-17-2010
tabstop
You would generally have a loop for each variable, kind of like an odometer (i.e., the right-most one will "spin" faster than the others, so you would check (1,1,1,2), then (1,1,1,3), then (1,1,1,4), then ..., then (1,1,1,50), then (1,1,2,1) etc.) Then once you have a candidate quadruple you need to check the conditions to see whether it "fits" and if so, then do whatever you need to do with it (print it, write to file, whatever).
• 03-17-2010
mayoussa89
Quote:

Originally Posted by tabstop
You would generally have a loop for each variable, kind of like an odometer (i.e., the right-most one will "spin" faster than the others, so you would check (1,1,1,2), then (1,1,1,3), then (1,1,1,4), then ..., then (1,1,1,50), then (1,1,2,1) etc.) Then once you have a candidate quadruple you need to check the conditions to see whether it "fits" and if so, then do whatever you need to do with it (print it, write to file, whatever).

can you please give me an example of an odemeter?
it would have some loop, where the limit is 50, right? am i on the right track?
i'm very confused now...
• 03-17-2010
tabstop
A real (trip) odometer:
Code:

```int hundreds, tens, ones, tenths; for (hundreds = 0; hundreds < 10; hundreds++) {     for (tens = 0; tens < 10; tens++) {         for (ones = 0; ones < 10; ones++) {             for (tenths = 0; tenths < 10; tenths++) {                 printf("%d%d%d.%d\n", hundreds, tens, ones, tenths);             }         }     } }```
• 03-17-2010
mayoussa89
Quote:

Originally Posted by tabstop
A real (trip) odometer:
Code:

```int hundreds, tens, ones, tenths; for (hundreds = 0; hundreds < 10; hundreds++) {     for (tens = 0; tens < 10; tens++) {         for (ones = 0; ones < 10; ones++) {             for (tenths = 0; tenths < 10; tenths++) {                 printf("%d%d%d.%d\n", hundreds, tens, ones, tenths);             }         }     } }```

ok i did as you said to, but i still cant seem to get it right...
since i want a+b+c+d=50, i made all my variables less than or equal to 50, correct?
and i used an if statement at the end to see if they meet the condition...
forgive me please for asking too many questions, im still new to c++ and i really appreciate your help a lot :)
Code:

```#include <iostream> #include <cmath> using namespace std; int main () {         int a, b, c, d;         for (a = 0; a <= 50; a++)                 for (b = 0; b <= 50; b++) {                         for (c = 0; c <= 50; c++) {                                 for (d = 0; d <= 10; d++) {                                         printf("%d%d%d.%d\n", a, b, c, d);             }         }     }                 if (a+b+c+d==50)                         cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<endl;         return 0; }```
• 03-17-2010
tabstop
At the end is after everything has happened, which is way way way way way way too late. You need to check each quadruple -- this is why I put the print statement where I did, not after all the loops had happened. (And no, there is no 50 anywhere in your problem.)
• 03-17-2010
iMalc
Your loops need only go up to < 15 since 15*15 is 256 which is over 225

You're pretty close. You only need to delete one line, change five numbers, move one line, and change an equation.