Reputation: 21
Below is the current program, everything checks out to me, except when I execute the program itself it gives me this error.
Fortran runtime error: Bad integer for item 1 in list input.
The first number is 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
Upvotes: 0
Views: 2079
Reputation: 2981
To expand on francescalus' and Vladimir F's comments:
The simplest way of debugging a program is adding a bunch of print
statements. If you add these to your code, like
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 = "
then you can see what your program is actually doing, and work out what's going wrong.
Your first problem is that the lines
do
read(1,*)firstnumber!reading the first number
end do
are performing the read
operation multiple times. It will literally try to read integers from the file forever. Obviously the file is finite, so eventually it runs out of valid things to read, and throws the error you are seeing.
To fix this, you should simply get rid of the infinite loop, by removing the lines do
and end do
.
Your next problem is the statement
read(i,*)
This reads from the file you just opened, but doesn't store the results anywhere. It looks like you're trying to populate the arrayone
array, which you would do like
read(i,*) arrayone(j)
where j
is the index of arrayone
corresponding to the element which you want to fill.
The other problem you have is that you're doing a lot of things inside a loop which you should be doing outside the loop. As it stands, the code
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
will:
arrayone
, even though most of the elements of arrayone
are not yet filled.and it will do all of this over and over again, firstnumber
times.
I suspect that what you want to do is read the firstnumber
elements of arrayone
from lines 2
through firstnumber+1
of the file, and only then (after the loop) calculate the sum, average, and standard deviation of arrayone
.
In order to read multiple rows from your file, you should only open it once (at the statement open(unit=1...
), and only close it once (using close(1)
, but at the end of your program). Each read
statement should then read from the same file (so each should be read(1,...
).
Upvotes: 2