For Programmers: Free Programming Magazines  


Home > Archive > Fortran > April 2005 > permutations/combinations









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author permutations/combinations
kanskje

2005-04-23, 4:20 pm

Hello!

I'm trying to write a program that outputs the possible combinations of a set of numbers, but I can't seem to get it right..

In a list of 1-2-3, the program only outputs 1-2-3.
It should be
1-2-3
1-3-2
2-1-3
2-3-1 etc...

Any suggestions? The code:

MODULE div

INTEGER, PARAMETER :: usedmax = 3
LOGICAL, DIMENSION(usedmax) :: used
INTEGER, DIMENSION(usedmax) :: choice

CONTAINS

RECURSIVE SUBROUTINE Useit()
INTEGER :: i, j

DO i = 1, usedmax, 1
IF (.NOT. used(i)) then
used(i) = .TRUE.
WRITE (*,*) 'made choice', choice(i)
CALL UseIt()
RETURN
ENDIF
ENDDO
RETURN

END SUBROUTINE UseIt

SUBROUTINE Init
IMPLICIT NONE
INTEGER :: i
DO i = 1, usedmax, 1
used(i) = .false.
choice(i) = i
ENDDO
RETURN

END SUBROUTINE init
END MODULE

PROGRAM main
USE div
IMPLICIT NONE
CALL Init
CALL UseIt()
STOP
END PROGRAM main
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com