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

This is a discussion on write a program that determines all the possible ways to divide an integer. within the C++ Programming forums, part of the General Programming Boards category; my program that im supposed to write needs to divide 200 hrs of work between four workers , based on ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    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;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    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...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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).

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Quote Originally Posted by tabstop View Post
    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...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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);
                }
            }
        }
    }

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Quote Originally Posted by tabstop View Post
    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;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 07:38 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Newbie needs help..
    By xpress urself in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:22 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21