Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NESTED LOOPS IN FORTRAN 77

Status
Not open for further replies.

LEBELGE

Programmer
Jul 5, 2007
1
ES
Hi, I have a simple problem for which I need nested loops with variable depth. As I don't know how to do that, so I have searched the net, and I do not understand the solutions they provide. Could someone eventually look at the following and help me ?
I wish to generate all the possible partitions of n objects into m different locations, with at least one object in every location. For instance, with 5 objects and 3 locations, one has 6 possibilities:
{1}{1}{3}, {1}{2}{2}, {1}{3}{1}, {2}{1}{2}, {2}{2}{1}, {3}{1}{1},

When m=4, the program would be:
do i1 = 1, n-m+1
do i2 = 1, n-i1-1
do i3 = 1, n-i1-i2-1
i4 = n-i1-i2-i3
write(*,*) i1, i2, i3, i4
enddo
enddo
enddo

But I would like to generalize this for any value of m that the user would ask for. How can I do that ?

Thank you for your help.
 
This question looks like a homework.
Use arrays to simulate nested loops stack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top