开发者

Need help with output of asterisks and indexes using arrays

The output of this program on fortran 95 displays asterisks instead of digits. Also I canno开发者_开发问答t get the Experiment# to print as intended like so; Experiment 1, Experiment 2, Experiment 3 and so on. Instead it prints as follows; Experiment 1, Experiment 1, Experiment 1.

Any ideas on how I can fix this issue? Below is my program in its entirety.

Thanks for your time.

PROGRAM numbersgen
    IMPLICIT NONE

        !Variable declaration
        INTEGER, DIMENSION(:,:),ALLOCATABLE::numarray
        INTEGER, DIMENSION(:),ALLOCATABLE::temparray
        INTEGER:: numrolls, numexps
        INTEGER:: i=0, j=0
        REAL:: avg=0, sdv=0, variance=0, sum=0
        INTEGER:: k, min, pos, temp

        .............
        ------

        REAL, INTENT(IN):: sum
        REAL, INTENT(IN):: avg, variance, sdv

        PRINT*, " "
        PRINT*, "Sum: ",sum
        PRINT '(1X,A,F5.3)', "Average: ",avg
        PRINT '(1X,A,F5.3)', "Variance: ",variance
        PRINT '(1X,A,F5.3)', "Standard Deviation: ",sdv

        END SUBROUTINE

END PROGRAM


The F5.3 format requires the value to be between 0 and 9.999. If the average is more than that, or negative, it splats instead. To find what a reasonable format specification is, temporarily change the formats to F15.3 so you can at least see the values.

I don't see why the experiment number fails to increment. Uh oh! Is the scope of i from the main program used in the subroutines?! There are no local declarations of them and implicit none is in effect, so I'm inclined to think this is a problem. An easy experiment to confirm would be to change the name of i in the main program to something totally different, like expidx, and see if there are compilation errors. (There are four places that need changing.)


By putting your subroutines inside a contain statement in the program, you give them access to the data that's declared in your program. As such, subroutines using i and j actually alter their values inside the program itself. Don't do this!

The 'proper' way would be to put your subroutines as separate program units or in a module and use it inside the main program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜