Fortran runtime error: Bad integer for item 1 in list input? Not sure why
Fortran runtime error: Bad integer for item 1 in list input? Not sure why
下面是当前程序,一切都对我进行了检查,除了当我执行程序本身时它给了我这个错误。
Fortran runtime error: Bad integer for item 1 in list input.
第一个数字是 60000。
implicit none
!declaring the single precision for real numbers, array, and variables.
integer :: i, firstnumber
real*4 :: average, sum_, standarddeviation
real*4, allocatable :: arrayone(:)!making sure that the array is single precision real
open(unit=1,file="C:\cygwin64\data sets for labs and assignments\Numbers.txt")!opening file
do
read(1,*)firstnumber!reading the first number
end do
allocate(arrayone(firstnumber))!allocating the array with the first number variable obtained from previous statement
close(1)
do i = 2, firstnumber
open(i,file="C:\cygwin64\data sets for labs and assignments\Numbers.txt")
read(i,*)
sum_ = Sum(arrayone) !sums all of the numbers from arrayone
average = Sum_/firstnumber
standarddeviation = sqrt(sum_ - average) * (sum_ - average)!calculates the standard deviation of the data set
close(i)
end do
print*,"Average = "
print*,"Standard Deviation = "
deallocate(arrayone)!Deallocating arrayone
print*,"Done!"
end program Assignmentthree
扩展 francescalus 和 Vladimir F 的评论:
捕捉错误
调试程序最简单的方法是添加一堆 print
语句。如果您将这些添加到您的代码中,例如
implicit none
!declaring the single precision for real numbers, array, and variables.
integer :: i, firstnumber
real*4 :: average, sum_, standarddeviation
real*4, allocatable :: arrayone(:)!making sure that the array is single precision real
open(unit=1,file="C:\cygwin64\data sets for labs and assignments\Numbers.txt")!opening file
do
read(1,*)firstnumber!reading the first number
print *, 'firstnumber = ', firstnumber
end do
allocate(arrayone(firstnumber))!allocating the array with the first number variable obtained from previous statement
close(1)
do i = 2, firstnumber
open(i,file="C:\cygwin64\data sets for labs and assignments\Numbers.txt")
read(i,*)
print *, 'arrayone = ', arrayone
sum_ = Sum(arrayone) !sums all of the numbers from arrayone
print *, 'sum_= ', sum_
average = Sum_/firstnumber
print *, 'average= ', average
standarddeviation = sqrt(sum_ - average) * (sum_ - average)!calculates the standard deviation of the data set
print *, 'standarddeviation= ', standarddeviation
close(i)
end do
print*,"Average = "
print*,"Standard Deviation = "
然后您就可以看到您的程序实际在做什么,并找出问题所在。
您当前的错误
你的第一个问题是行
do
read(1,*)firstnumber!reading the first number
end do
正在多次执行 read
操作。它会从字面上尝试永远从文件中读取整数。显然文件是有限的,所以最终它用完了有效的东西来读取,并抛出你看到的错误。
要解决此问题,您只需删除行 do
和 end do
.
即可摆脱无限循环
空读
你的下一个问题是语句
read(i,*)
这将从您刚刚打开的文件中读取,但不会将结果存储在任何地方。看起来您正在尝试填充 arrayone
数组,您会这样做
read(i,*) arrayone(j)
其中 j
是 arrayone
对应要填充的元素的索引。
循环中
你遇到的另一个问题是你在循环内做了很多你应该在循环外做的事情。就目前而言,代码
do i = 2, firstnumber
open(i,file="C:\cygwin64\data sets for labs and assignments\Numbers.txt")
read(i,*)
sum_ = Sum(arrayone)
average = Sum_/firstnumber
standarddeviation = sqrt(sum_ - average) * (sum_ - average)
close(i)
end do
将:
- 打开文件
- 读取文件中的第一个数字,但不将其存储在任何地方
- 计算整个数组
arrayone
的总和、平均值和标准差,即使arrayone
的大部分元素还没有填满。
- 关闭文件
它会一遍又一遍地执行所有这些操作,firstnumber
次。
我怀疑您想要做的是从文件的第 2
行到 firstnumber+1
行读取 arrayone
的 firstnumber
元素,然后才(之后循环)计算 arrayone
.
的总和、平均值和标准差
为了从你的文件中读取多行,你应该只打开它一次(在语句open(unit=1...
),并且只关闭它一次(使用close(1)
,但是在你的程序)。每个 read
语句应该从同一个文件中读取(所以每个应该是 read(1,...
)。
下面是当前程序,一切都对我进行了检查,除了当我执行程序本身时它给了我这个错误。
Fortran runtime error: Bad integer for item 1 in list input.
第一个数字是 60000。
implicit none
!declaring the single precision for real numbers, array, and variables.
integer :: i, firstnumber
real*4 :: average, sum_, standarddeviation
real*4, allocatable :: arrayone(:)!making sure that the array is single precision real
open(unit=1,file="C:\cygwin64\data sets for labs and assignments\Numbers.txt")!opening file
do
read(1,*)firstnumber!reading the first number
end do
allocate(arrayone(firstnumber))!allocating the array with the first number variable obtained from previous statement
close(1)
do i = 2, firstnumber
open(i,file="C:\cygwin64\data sets for labs and assignments\Numbers.txt")
read(i,*)
sum_ = Sum(arrayone) !sums all of the numbers from arrayone
average = Sum_/firstnumber
standarddeviation = sqrt(sum_ - average) * (sum_ - average)!calculates the standard deviation of the data set
close(i)
end do
print*,"Average = "
print*,"Standard Deviation = "
deallocate(arrayone)!Deallocating arrayone
print*,"Done!"
end program Assignmentthree
扩展 francescalus 和 Vladimir F 的评论:
捕捉错误
调试程序最简单的方法是添加一堆 print
语句。如果您将这些添加到您的代码中,例如
implicit none
!declaring the single precision for real numbers, array, and variables.
integer :: i, firstnumber
real*4 :: average, sum_, standarddeviation
real*4, allocatable :: arrayone(:)!making sure that the array is single precision real
open(unit=1,file="C:\cygwin64\data sets for labs and assignments\Numbers.txt")!opening file
do
read(1,*)firstnumber!reading the first number
print *, 'firstnumber = ', firstnumber
end do
allocate(arrayone(firstnumber))!allocating the array with the first number variable obtained from previous statement
close(1)
do i = 2, firstnumber
open(i,file="C:\cygwin64\data sets for labs and assignments\Numbers.txt")
read(i,*)
print *, 'arrayone = ', arrayone
sum_ = Sum(arrayone) !sums all of the numbers from arrayone
print *, 'sum_= ', sum_
average = Sum_/firstnumber
print *, 'average= ', average
standarddeviation = sqrt(sum_ - average) * (sum_ - average)!calculates the standard deviation of the data set
print *, 'standarddeviation= ', standarddeviation
close(i)
end do
print*,"Average = "
print*,"Standard Deviation = "
然后您就可以看到您的程序实际在做什么,并找出问题所在。
您当前的错误
你的第一个问题是行
do
read(1,*)firstnumber!reading the first number
end do
正在多次执行 read
操作。它会从字面上尝试永远从文件中读取整数。显然文件是有限的,所以最终它用完了有效的东西来读取,并抛出你看到的错误。
要解决此问题,您只需删除行 do
和 end do
.
空读
你的下一个问题是语句
read(i,*)
这将从您刚刚打开的文件中读取,但不会将结果存储在任何地方。看起来您正在尝试填充 arrayone
数组,您会这样做
read(i,*) arrayone(j)
其中 j
是 arrayone
对应要填充的元素的索引。
循环中
你遇到的另一个问题是你在循环内做了很多你应该在循环外做的事情。就目前而言,代码
do i = 2, firstnumber
open(i,file="C:\cygwin64\data sets for labs and assignments\Numbers.txt")
read(i,*)
sum_ = Sum(arrayone)
average = Sum_/firstnumber
standarddeviation = sqrt(sum_ - average) * (sum_ - average)
close(i)
end do
将:
- 打开文件
- 读取文件中的第一个数字,但不将其存储在任何地方
- 计算整个数组
arrayone
的总和、平均值和标准差,即使arrayone
的大部分元素还没有填满。 - 关闭文件
它会一遍又一遍地执行所有这些操作,firstnumber
次。
我怀疑您想要做的是从文件的第 2
行到 firstnumber+1
行读取 arrayone
的 firstnumber
元素,然后才(之后循环)计算 arrayone
.
为了从你的文件中读取多行,你应该只打开它一次(在语句open(unit=1...
),并且只关闭它一次(使用close(1)
,但是在你的程序)。每个 read
语句应该从同一个文件中读取(所以每个应该是 read(1,...
)。