Small FORTRAN code for combinations. BUG

In summary, the conversation was about a small FORTRAN code for combinations that had a bug. The code was used to print all combinations of numbers 1-9 in sets of r, but it was printing an extra line for r=2 and r=3. The solution was to put the WRITE(*,*)comb statement under the condition IF(j.EQ.r). This solved the issue and the code was able to successfully print all combinations without any extra lines.
  • #1
jajabinker
8
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
  • #2


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:
  • #3


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

Related to Small FORTRAN code for combinations. BUG

1. What is a FORTRAN code for combinations?

A FORTRAN code for combinations is a program written in the FORTRAN programming language to calculate the number of combinations of a given set of objects. It is commonly used in mathematical and scientific calculations.

2. How does the FORTRAN code for combinations work?

The FORTRAN code for combinations uses a mathematical formula known as the combination formula to calculate the number of combinations. The formula is nCr = n! / (r!(n-r)!), where n represents the total number of objects and r represents the number of objects chosen for each combination.

3. What are the benefits of using FORTRAN for calculating combinations?

FORTRAN is a high-level and efficient programming language that is specifically designed for scientific and numerical calculations. It has built-in functions for mathematical operations, making it easier to write and execute codes for complex calculations like combinations.

4. What is the most common bug in FORTRAN code for combinations?

The most common bug in FORTRAN code for combinations is using the wrong input values for the combination formula. This can lead to incorrect results or errors in the program. It is important to double check the input values and ensure they are in the correct format before running the code.

5. How can I avoid bugs in my FORTRAN code for combinations?

To avoid bugs in FORTRAN code for combinations, it is important to thoroughly test the program and check for any errors before running it. It is also helpful to use comments and descriptive variable names to make the code more readable and easier to debug. Additionally, understanding the combination formula and its implementation in FORTRAN can help identify potential errors in the code.

Similar threads

  • Programming and Computer Science
Replies
4
Views
693
  • Programming and Computer Science
Replies
12
Views
994
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top