需要使用数组输出星号和索引的帮助

发布于 2024-11-01 02:33:53 字数 842 浏览 4 评论 0原文

该程序在 Fortran 95 上的输出显示星号而不是数字。我也无法让实验#按预期打印;实验1、实验2、实验3等等。相反,它打印如下;实验 1,实验 1,实验 1。

关于如何解决此问题有什么想法吗?以下是我的完整程序。

感谢您抽出时间。

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 output of this program on fortran 95 displays asterisks instead of digits. Also I cannot 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

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

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

发布评论

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

评论(2

ぃ双果 2024-11-08 02:33:53

F5.3 格式要求值介于 0 到 9.999 之间。如果平均值大于该值或为负值,则会出现“啪啪”声。要找到合理的格式规范,请暂时将格式更改为 F15.3,以便您至少可以看到这些值。

我不明白为什么实验编号无法增加。 呃哦!主程序中的i的范围是否在子例程中使用?!它们没有本地声明,并且隐式没有有效,所以我倾向于认为这是一个问题。一个简单的确认实验是将主程序中的i名称更改为完全不同的名称,例如expidx,并查看是否存在编译错误。 (有四个地方需要修改。)

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.)

看轻我的陪伴 2024-11-08 02:33:53

通过将子例程放入程序的包含语句中,您可以让它们访问程序中声明的数据。因此,使用 i 和 j 的子例程实际上会改变程序本身内部的值。不要这样做!

“正确”的方法是将子例程作为单独的程序单元或模块放在一个模块中,并在主程序中使用它。

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.

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