需要使用数组输出星号和索引的帮助
该程序在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 toF15.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 ofi
from the main program used in the subroutines?! There are no local declarations of them andimplicit 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 ofi
in the main program to something totally different, likeexpidx
, and see if there are compilation errors. (There are four places that need changing.)通过将子例程放入程序的包含语句中,您可以让它们访问程序中声明的数据。因此,使用 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.