本文介绍了用子程序输入数组文件(.dat)的最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现一个读取数字n的代码,创建一个存储n个双精度数字的向量,读取此数字,调用子程序printminmax()以查找最小值和最大值。我的代码适用于正常数字(整数,实数等),但是当我有科学记数法(0.3412E + 01)时,为什么?我认为与*阅读所有格式。谢谢

I try to implement a code that read in a number n, creates a vector to store n double precision numbers, read this number, call a subroutine printminmax() to find min and max. My code work perfect for normal numbers (integer,real etc) but when i have scientific notation (0.3412E+01) stack.Why? I thought with * read all the formats. Thanks

implicit none

integer, dimension(:), allocatable :: x
    integer :: n

    open (unit=77, file='input2.dat', action='read', status='old')
    read(77,*), n
    allocate(x(n))

    call printminmax(n)

    deallocate(x)

    end 

 subroutine printminmax(y)

implicit none

integer, dimension(:), allocatable :: x
integer :: y,max,min,i
   allocate(x(y))
   read(77,*) x
   !print *,'Maximun=', maxval(x)

   !print *,'Minimun=', minval(x

   !initialize the value max & min
max=x(1)
min=x(1)
do i=2,y
    if (x(i)>max) max=x(i)
    if (x(i)<min) min=x(i)
end do
    write(*,*) 'Maximum=',max
write(*,*) 'Minimum=',min

    end subroutine printminmax 

堆栈输入的一个例子是

one example of the stack input is

  1000
  5.39524398466520e-01
  9.85099770130787e-01
  7.38946122872518e-01
  6.47771620257608e-01
  8.80871051119695e-01
  2.99375585725816e-02

我对科学记数法的错误是

the error that i take for scientific notation is

   At line 13 of file io.f90 (unit = 77, file = 'input3.dat')
   Fortran runtime error: Bad integer for item 1 in list input


推荐答案

在x上有双精度,没有整数。

ok i found it.I should have double precision on x, no integer.

这篇关于用子程序输入数组文件(.dat)的最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 08:51