Fortran 中的运行时错误

发布于 2024-12-12 03:16:35 字数 6709 浏览 0 评论 0原文

我的 FORTRAN 语言处于初级水平。最近,我尝试设计一个简单的程序来计算排列和组合。它将有一个“菜单”供用户选择继续的选项...

      !/*-------MAIN PROGRAM STARTS-------*/
      !PURPOSE: TO CALCULATE COMBINATIONS AND PERMUTATIONS
      PROGRAM COMBINATION_PERMUTATION
      IMPLICIT NONE

      !DECLARATION OF VARIABLES
      REAL N, K, FACT, COMBINATION, PERMUTATION
      INTEGER CHOICE

      !CHOICE MENU
      CALL MENU

      !USER INPUT OF CHOICE
      READ(*,*) CHOICE

      !EXECUTION OF CODE BASED ON USER'S CHOICE
      SELECT CASE(CHOICE)
        CASE(1)
            !CALLS SUBROUTINE FOR COMBINATION 
            CALL COMBINATION_CALC (N, K, FACT, COMBINATION)            
        CASE(2)
            !CALLS SUBROUTINE FOR PERMUTATION
            CALL PERMUTATION_CALC (N, K, FACT, PERMUTATION)
        CASE(3)
            !EXITS THE PROGRAM
            STOP 
        CASE DEFAULT
        !INVALID CHOICE LEADS TO MENU BACK
        WRITE(*,*) 'INVALID CHOICE! PLEASE CHOOSE AN OPTION TO CONTINUE'
        CALL MENU
      END SELECT

      END PROGRAM COMBINATION_PERMUTATION
      !/*-------MAIN PROGRAM ENDS-------*/


      !/*-------SUB PROGRAMS START-------*/
      !FUNCTION FOR FACTORIAL
      FUNCTION FACT(N)
      IMPLICIT NONE
      REAL FACT, N
      INTEGER P, I
      P=1
      DO I=1, N
        P=P*I
      END DO
      FACT=P
      RETURN
      END FUNCTION 

      !SUBROUTINE FOR MENU
      SUBROUTINE MENU
      IMPLICIT NONE
      WRITE(*,*) ' CHOOSE AN OPTION TO CONTINUE.. '
      WRITE(*,*) ' 1. COMBINATION '
      WRITE(*,*) ' 2. PERMUTATION '
      WRITE(*,*) ' 3. EXIT PROGRAM '
      RETURN
      END SUBROUTINE MENU

      !SUBROUTINE FOR COMBINATION
      SUBROUTINE COMBINATION_CALC (N, K, FACT, COMBINATION)
      IMPLICIT NONE

      REAL N, K, FACT, COMBINATION

      WRITE(*,*) ' WHAT IS THE VALUE OF N? '
      READ(*,*) N

2     WRITE(*,*) ' WHAT IS THE VALUE OF K? '
      READ(*,*) K

      IF (K<0) THEN 
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
            GO TO 2
        ELSE IF (K>N)THEN
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
            GO TO 2
        ELSE
      END IF 

      COMBINATION=FACT(N)/(FACT(N-K)*FACT(N))
      WRITE(*,*) ' HENCE, THERE ARE ', COMBINATION, ' WAYS TO ARRANGE. '
      CALL MENU
      RETURN
      END SUBROUTINE COMBINATION_CALC

      !SUBROUTINE FOR PERMUTATION
      SUBROUTINE PERMUTATION_CALC (N, K, FACT, PERMUTATION)
      IMPLICIT NONE

      REAL N, K, FACT, PERMUTATION

      WRITE(*,*) ' WHAT IS THE VALUE OF N? '
      READ(*,*) N

3     WRITE(*,*) ' WHAT IS THE VALUE OF K? '
      READ(*,*) K

      IF (K<0) THEN 
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
            GO TO 3
        ELSE IF (K>N) THEN
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
            GO TO 3
        ELSE
      END IF

      PERMUTATION=FACT(N)/FACT(N-K)
      WRITE(*,*) ' HENCE THERE ARE', PERMUTATION,' WAYS TO ARRANGE. '
      CALL MENU      
      RETURN
      END SUBROUTINE PERMUTATION_CALC
      !/*-------SUB PROGRAMS END-------*/

每当我输入输入 1 或 2 以选择要继续的情况时,我都会收到运行时错误...

错误显示如下在弹出窗口中:-


运行时错误

当需要过程时,尝试调用参数号为 3 的例程(kind=1)。

COMBINATION_CALC- 在文件combinationpermutation.for第62行[+0068] main- 在文件combinationpermutation.for第23行[+00e2]


据我所知,COMBINATION_CALC中的第三个参数是“FACT”,它是计算阶乘的函数。我无法理解这个错误。请指导我。预先感谢..


更新

我已经使用 yosukesabai 给出的修复程序修复了运行时错误...我的程序现已完成..但是,整数溢出对我来说是一个新问题。我已将 REAL 声明更改为 INTEGER(KIND=4)。当 N 值和 K 值大于 12 时...组合和排列的结果答案将会很大...我可以知道如何解决这个问题吗?

这是我的程序的新代码......

          !/*-------MAIN PROGRAM STARTS-------*/
      !PURPOSE: TO CALCULATE COMBINATIONS AND PERMUTATIONS
      !CREATED BY: RETHNARAJ RAMBABU
      !DATE: 28/10/2011
      PROGRAM COMBINATION_PERMUTATION
      IMPLICIT NONE

      !DECLARATION OF VARIABLES
      INTEGER CHOICE, N, K
      INTEGER(KIND=4) COMBINATION, PERMUTATION
      INTEGER, external :: FACT

      !CHOICE MENU
1     WRITE(*,*) ' CHOOSE AN OPTION TO CONTINUE.. '
      WRITE(*,*) ' 1. COMBINATION '
      WRITE(*,*) ' 2. PERMUTATION '
      WRITE(*,*) ' 3. EXIT PROGRAM '

      !USER INPUT OF CHOICE
      READ(*,*) CHOICE

      !EXECUTION OF CODE BASED ON USER'S CHOICE
      SELECT CASE(CHOICE)
        CASE(1)
            !CALLS SUBROUTINE FOR COMBINATION 
            CALL COMBINATION_CALC (N, K, FACT, COMBINATION)  
            GO TO 1          
        CASE(2)
            !CALLS SUBROUTINE FOR PERMUTATION
            CALL PERMUTATION_CALC (N, K, FACT, PERMUTATION)
            GO TO 1
        CASE(3)
            !EXITS THE PROGRAM
            STOP 
        CASE DEFAULT
        !INVALID CHOICE LEADS TO MENU BACK
        WRITE(*,*) 'INVALID CHOICE! PLEASE CHOOSE AN OPTION TO CONTINUE'
        GO TO 1
      END SELECT

      END PROGRAM COMBINATION_PERMUTATION
      !/*-------MAIN PROGRAM ENDS-------*/


      !/*-------SUB PROGRAMS START-------*/
      !FUNCTION FOR FACTORIAL
      FUNCTION FACT(N)
      IMPLICIT NONE
      INTEGER N, P, I, FACT
      P=1
      DO I=1, N
        P=P*I
      END DO
      FACT=P
      RETURN
      END FUNCTION 

      !SUBROUTINE FOR COMBINATION
      SUBROUTINE COMBINATION_CALC (N, K, FACT, COMBINATION)
      IMPLICIT NONE

      INTEGER N, K, FACT
      INTEGER(KIND=4) COMBINATION

      WRITE(*,*) ' WHAT IS THE VALUE OF N? '
      READ(*,*) N

2     WRITE(*,*) ' WHAT IS THE VALUE OF K? '
      READ(*,*) K

      IF (K<0) THEN 
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
            GO TO 2
        ELSE IF (K>N)THEN
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
            GO TO 2
        ELSE
      END IF 

      COMBINATION=FACT(N)/(FACT(N-K)*FACT(K))
      WRITE(*,*) ' HENCE, THERE ARE ', COMBINATION, ' WAYS TO ARRANGE. '
      RETURN
      END SUBROUTINE COMBINATION_CALC

      !SUBROUTINE FOR PERMUTATION
      SUBROUTINE PERMUTATION_CALC (N, K, FACT, PERMUTATION)
      IMPLICIT NONE

      INTEGER N, K, FACT
      INTEGER(KIND=4)PERMUTATION

      WRITE(*,*) ' WHAT IS THE VALUE OF N? '
      READ(*,*) N

3     WRITE(*,*) ' WHAT IS THE VALUE OF K? '
      READ(*,*) K

      IF (K<0) THEN 
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
            GO TO 3
        ELSE IF (K>N) THEN
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
            GO TO 3
        ELSE
      END IF

      PERMUTATION=FACT(N)/FACT(N-K)
      WRITE(*,*) ' HENCE THERE ARE', PERMUTATION,' WAYS TO ARRANGE. '
      RETURN
      END SUBROUTINE PERMUTATION_CALC
      !/*-------SUB PROGRAMS END-------*/

I'm at a beginner level in FORTRAN. Recently, I tried designing a simple program to calculate permutations and combinations. It will have a "menu" for the user to choose an option to proceed...

      !/*-------MAIN PROGRAM STARTS-------*/
      !PURPOSE: TO CALCULATE COMBINATIONS AND PERMUTATIONS
      PROGRAM COMBINATION_PERMUTATION
      IMPLICIT NONE

      !DECLARATION OF VARIABLES
      REAL N, K, FACT, COMBINATION, PERMUTATION
      INTEGER CHOICE

      !CHOICE MENU
      CALL MENU

      !USER INPUT OF CHOICE
      READ(*,*) CHOICE

      !EXECUTION OF CODE BASED ON USER'S CHOICE
      SELECT CASE(CHOICE)
        CASE(1)
            !CALLS SUBROUTINE FOR COMBINATION 
            CALL COMBINATION_CALC (N, K, FACT, COMBINATION)            
        CASE(2)
            !CALLS SUBROUTINE FOR PERMUTATION
            CALL PERMUTATION_CALC (N, K, FACT, PERMUTATION)
        CASE(3)
            !EXITS THE PROGRAM
            STOP 
        CASE DEFAULT
        !INVALID CHOICE LEADS TO MENU BACK
        WRITE(*,*) 'INVALID CHOICE! PLEASE CHOOSE AN OPTION TO CONTINUE'
        CALL MENU
      END SELECT

      END PROGRAM COMBINATION_PERMUTATION
      !/*-------MAIN PROGRAM ENDS-------*/


      !/*-------SUB PROGRAMS START-------*/
      !FUNCTION FOR FACTORIAL
      FUNCTION FACT(N)
      IMPLICIT NONE
      REAL FACT, N
      INTEGER P, I
      P=1
      DO I=1, N
        P=P*I
      END DO
      FACT=P
      RETURN
      END FUNCTION 

      !SUBROUTINE FOR MENU
      SUBROUTINE MENU
      IMPLICIT NONE
      WRITE(*,*) ' CHOOSE AN OPTION TO CONTINUE.. '
      WRITE(*,*) ' 1. COMBINATION '
      WRITE(*,*) ' 2. PERMUTATION '
      WRITE(*,*) ' 3. EXIT PROGRAM '
      RETURN
      END SUBROUTINE MENU

      !SUBROUTINE FOR COMBINATION
      SUBROUTINE COMBINATION_CALC (N, K, FACT, COMBINATION)
      IMPLICIT NONE

      REAL N, K, FACT, COMBINATION

      WRITE(*,*) ' WHAT IS THE VALUE OF N? '
      READ(*,*) N

2     WRITE(*,*) ' WHAT IS THE VALUE OF K? '
      READ(*,*) K

      IF (K<0) THEN 
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
            GO TO 2
        ELSE IF (K>N)THEN
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
            GO TO 2
        ELSE
      END IF 

      COMBINATION=FACT(N)/(FACT(N-K)*FACT(N))
      WRITE(*,*) ' HENCE, THERE ARE ', COMBINATION, ' WAYS TO ARRANGE. '
      CALL MENU
      RETURN
      END SUBROUTINE COMBINATION_CALC

      !SUBROUTINE FOR PERMUTATION
      SUBROUTINE PERMUTATION_CALC (N, K, FACT, PERMUTATION)
      IMPLICIT NONE

      REAL N, K, FACT, PERMUTATION

      WRITE(*,*) ' WHAT IS THE VALUE OF N? '
      READ(*,*) N

3     WRITE(*,*) ' WHAT IS THE VALUE OF K? '
      READ(*,*) K

      IF (K<0) THEN 
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
            GO TO 3
        ELSE IF (K>N) THEN
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
            GO TO 3
        ELSE
      END IF

      PERMUTATION=FACT(N)/FACT(N-K)
      WRITE(*,*) ' HENCE THERE ARE', PERMUTATION,' WAYS TO ARRANGE. '
      CALL MENU      
      RETURN
      END SUBROUTINE PERMUTATION_CALC
      !/*-------SUB PROGRAMS END-------*/

And I'm getting run-time error whenever I enter input 1 or 2 to select a case to proceed....

The error shows as follows in a pop-up window:-


RUN TIME ERROR

Attempt to call a routine with argument number three as real(kind=1) when a procedure was required.

COMBINATION_CALC- in file combinationpermutation.for at line 62[+0068]
main- in file combinationpermutation.for at line 23 [+00e2]


As far as I know, the argument number three in COMBINATION_CALC is "FACT" which is function to calculate factorial. I am not able to understand the error. Kindly guide me. Thanks in advance..


UPDATE

I've fixed the RUN-TIME ERROR using the fix given by yosukesabai... my program is now complete..but, INTEGER OVERFLOW is a new problem for me. I've changed REAL declaration to INTEGER(KIND=4).. When the N value and K value are more than 12... the resulting answer for combination and permutation will be large... May I know how to fix this?

Here is the new code for my program...

          !/*-------MAIN PROGRAM STARTS-------*/
      !PURPOSE: TO CALCULATE COMBINATIONS AND PERMUTATIONS
      !CREATED BY: RETHNARAJ RAMBABU
      !DATE: 28/10/2011
      PROGRAM COMBINATION_PERMUTATION
      IMPLICIT NONE

      !DECLARATION OF VARIABLES
      INTEGER CHOICE, N, K
      INTEGER(KIND=4) COMBINATION, PERMUTATION
      INTEGER, external :: FACT

      !CHOICE MENU
1     WRITE(*,*) ' CHOOSE AN OPTION TO CONTINUE.. '
      WRITE(*,*) ' 1. COMBINATION '
      WRITE(*,*) ' 2. PERMUTATION '
      WRITE(*,*) ' 3. EXIT PROGRAM '

      !USER INPUT OF CHOICE
      READ(*,*) CHOICE

      !EXECUTION OF CODE BASED ON USER'S CHOICE
      SELECT CASE(CHOICE)
        CASE(1)
            !CALLS SUBROUTINE FOR COMBINATION 
            CALL COMBINATION_CALC (N, K, FACT, COMBINATION)  
            GO TO 1          
        CASE(2)
            !CALLS SUBROUTINE FOR PERMUTATION
            CALL PERMUTATION_CALC (N, K, FACT, PERMUTATION)
            GO TO 1
        CASE(3)
            !EXITS THE PROGRAM
            STOP 
        CASE DEFAULT
        !INVALID CHOICE LEADS TO MENU BACK
        WRITE(*,*) 'INVALID CHOICE! PLEASE CHOOSE AN OPTION TO CONTINUE'
        GO TO 1
      END SELECT

      END PROGRAM COMBINATION_PERMUTATION
      !/*-------MAIN PROGRAM ENDS-------*/


      !/*-------SUB PROGRAMS START-------*/
      !FUNCTION FOR FACTORIAL
      FUNCTION FACT(N)
      IMPLICIT NONE
      INTEGER N, P, I, FACT
      P=1
      DO I=1, N
        P=P*I
      END DO
      FACT=P
      RETURN
      END FUNCTION 

      !SUBROUTINE FOR COMBINATION
      SUBROUTINE COMBINATION_CALC (N, K, FACT, COMBINATION)
      IMPLICIT NONE

      INTEGER N, K, FACT
      INTEGER(KIND=4) COMBINATION

      WRITE(*,*) ' WHAT IS THE VALUE OF N? '
      READ(*,*) N

2     WRITE(*,*) ' WHAT IS THE VALUE OF K? '
      READ(*,*) K

      IF (K<0) THEN 
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
            GO TO 2
        ELSE IF (K>N)THEN
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
            GO TO 2
        ELSE
      END IF 

      COMBINATION=FACT(N)/(FACT(N-K)*FACT(K))
      WRITE(*,*) ' HENCE, THERE ARE ', COMBINATION, ' WAYS TO ARRANGE. '
      RETURN
      END SUBROUTINE COMBINATION_CALC

      !SUBROUTINE FOR PERMUTATION
      SUBROUTINE PERMUTATION_CALC (N, K, FACT, PERMUTATION)
      IMPLICIT NONE

      INTEGER N, K, FACT
      INTEGER(KIND=4)PERMUTATION

      WRITE(*,*) ' WHAT IS THE VALUE OF N? '
      READ(*,*) N

3     WRITE(*,*) ' WHAT IS THE VALUE OF K? '
      READ(*,*) K

      IF (K<0) THEN 
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
            GO TO 3
        ELSE IF (K>N) THEN
            WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
            GO TO 3
        ELSE
      END IF

      PERMUTATION=FACT(N)/FACT(N-K)
      WRITE(*,*) ' HENCE THERE ARE', PERMUTATION,' WAYS TO ARRANGE. '
      RETURN
      END SUBROUTINE PERMUTATION_CALC
      !/*-------SUB PROGRAMS END-------*/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

复古式 2024-12-19 03:16:35

在代码顶部附近,

  PROGRAM COMBINATION_PERMUTATION
  IMPLICIT NONE

  !DECLARATION OF VARIABLES
  REAL N, K, FACT, COMBINATION, PERMUTATION
  INTEGER CHOICE

您在这里说 FACT 是实数,因此 fortran 假设它是实数变量。但您的意思是这是对您在同一文件中定义的函数的引用。因此,像 external 一样修改

  !DECLARATION OF VARIABLES
  REAL N, K, COMBINATION, PERMUTATION
  INTEGER CHOICE
  REAL, external :: FACT

是 fortran 的说法,它是您在程序块(程序、子例程或函数)之外的某个地方定义的函数


(如果您不关心,请忽略下面的内容)

我注意到传递函数引用是没有意义的。所以你可能想停止传递函数,而直接从子例程combination_calc中调用它。在这种情况下,n,k,combination 是子例程的三个参数,并且在combination_calc 中您将拥有real, external::fact

Near the top of your code,

  PROGRAM COMBINATION_PERMUTATION
  IMPLICIT NONE

  !DECLARATION OF VARIABLES
  REAL N, K, FACT, COMBINATION, PERMUTATION
  INTEGER CHOICE

Here you said FACT is real, so fortran assumes that it is a real variable. But you meant this to be reference to function you defined in the same file. So revise like

  !DECLARATION OF VARIABLES
  REAL N, K, COMBINATION, PERMUTATION
  INTEGER CHOICE
  REAL, external :: FACT

external is fortran way of saying that it is a function you defnined somewhere outside of a program block (program, subroutine or function)


(ignore below here if you dont care)

I notice that there is no point of passing function reference. so you may want to stop passing function around, and just call that straight from subroutine combination_calc. in that case, n,k,combination are the three arguments for subroutine, and inside combination_calc you will have real, external :: fact.

就是爱搞怪 2024-12-19 03:16:35

关于INTEGER溢出,将变量P(函数FACT内)声明为REAL而不是INTEGER。 INTEGER(4) 变量不能超过 2^3112! < 2^31 < 13!

但请注意:实数值也可能发生溢出(在我的计算机上的 35! 处:34!=2.9523282E+38) 。对于双精度 (REAL(8)),溢出发生在 171! (170!= 7.2574156153+306)

About the INTEGER overflow, declare the variable P (within the function FACT) REAL instead of INTEGER. An INTEGER(4) variable cannot exceed 2^31 and 12! < 2^31 < 13!

But take care : an overflow may occur with a REAL value as well (at 35! on my computer : 34!=2.9523282E+38). With a DOUBLE PRECISION (REAL(8)), the overflow occurs at 171! (170! = 7.2574156153+306)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文