Small FORTRAN code for combinations. BUG

Click For Summary
SUMMARY

The discussion centers on a bug in a FORTRAN program designed to generate combinations. The original code incorrectly prints extra lines when generating combinations for specified values of n and r. The solution involves moving the WRITE statement to only execute when the condition j.EQ.r is met. The corrected code successfully generates combinations of numbers from 1 to 9 in sets of r, with r set to 4 and n set to 32.

PREREQUISITES
  • Understanding of FORTRAN programming language
  • Familiarity with recursive subroutines
  • Knowledge of combinatorial mathematics
  • Experience with dynamic memory allocation in FORTRAN
NEXT STEPS
  • Explore advanced FORTRAN features in version 2008
  • Learn about memory management and allocation in FORTRAN
  • Study combinatorial algorithms and their implementations
  • Investigate debugging techniques for recursive functions in FORTRAN
USEFUL FOR

This discussion is beneficial for FORTRAN developers, programmers working on combinatorial algorithms, and anyone interested in optimizing recursive code in FORTRAN.

jajabinker
Messages
8
Reaction score
1
small FORTRAN code for combinations. BUG!

Code:
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
1 2
1 3
1 4
1 4 <-here
2 3
2 4
2 4 <-here
3 4
3 4 <-here

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:
Technology news on Phys.org


NVM solved it, Silly :smile:

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:
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:


For future reference, use [noparse]
Code:
...
[/noparse] tags around your code, not quote tags. I did this in your posts.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K