Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Small FORTRAN code for combinations. BUG

  1. Nov 9, 2011 #1
    small FORTRAN code for combinations. BUG!!!

    Code (Text):

    PROGRAM test
    implicit double precision(a-h,o-z)

    INTEGER :: r=2,n=4,k=1
    INTEGER,allocatable :: comb(:)           !array to hold a set of combinations
            allocate(comb(r))
            comb(1)=1
            CALL iterate(1,n-r+1,1)
    CONTAINS

    RECURSIVE SUBROUTINE iterate(s,e,j)
    INTEGER,INTENT(IN) :: s,e
            DO i=s,e
             comb(j)=i
             if(j.LT.r) then
              CALL iterate(i+1,e+1,j+1)
             END IF
             WRITE(*,*)comb
            END DO
    END SUBROUTINE iterate

    END PROGRAM test
     
    for n=4,r=2
    prints
    For any n it prints and extra line once for r=2.

    For r=3 it prints extra twice.

    can anyone help?
     
    Last edited by a moderator: Nov 9, 2011
  2. jcsd
  3. Nov 9, 2011 #2
    Re: small FORTRAN code for combinations. BUG!!!

    NVM solved it, Silly :rofl:

    put the WRITE(*,*)comb statement under the following IF condition.

    IF(j.EQ.r) WRITE(*,*)comb

    Just for reference this is the working code to print all combinations of 1-9 in sets of r.
    Code (Text):

    PROGRAM combinations

    INTEGER :: r=4,n=32
    INTEGER,allocatable :: comb(:)

        allocate(comb(r))
        CALL iterate(1,n-r+1,1)
    CONTAINS

    RECURSIVE SUBROUTINE iterate(s,e,j)
    INTEGER,INTENT(IN) :: s,e  
        DO i=s,e
             comb(j)=i
             IF(j.LT.r) CALL iterate(i+1,e+1,j+1)
         IF(j.EQ.r) WRITE(*,*)comb
         END IF
        END DO
    END SUBROUTINE iterate

    END PROGRAM test
     
     
    Last edited by a moderator: Nov 9, 2011
  4. Nov 9, 2011 #3

    Mark44

    Staff: Mentor

    Re: small FORTRAN code for combinations. BUG!!!

    For future reference, use [noparse]
    Code (Text):
    ...
    [/noparse] tags around your code, not quote tags. I did this in your posts.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Small FORTRAN code for combinations. BUG
  1. Fortran 77 output bugs (Replies: 2)

  2. Help with fortran code (Replies: 17)

  3. Help with fortran code (Replies: 4)

Loading...