0

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
Nik Wrye
  • 21
  • 6
  • 1
    It seems you are learning as a Fortran beginner. Be advised that `real*4` is not a standard Fortran syntax, but a common non-standard extension. You should also make some own effort for debugging. Try to always print what you just read and diagnose what happens from that. – Vladimir F Героям слава Sep 14 '21 at 22:09
  • 3
    You try to read the `firstnumber` a large number of times, not once, because it's in a indefinite loop. That can't be what you mean? And with `read(i,*)` later you aren't reading in to any variable. Finally, opening the same file in different units is not the same as reading subsequent lines. – francescalus Sep 14 '21 at 22:09
  • @VladimirF we have not learned how to debug a program, but the professor specifically asked that we use real*4; however, how does this relate to my question? – Nik Wrye Sep 14 '21 at 22:26
  • @francescalus So you are saying that I am trying to use firstnumber to much and how does that make the first integer in the open file, 60000, a "bad integer"? Also, if I state that i=2 before the do loop it should be able to read in subsequent lines? – Nik Wrye Sep 14 '21 at 22:27
  • Sorry to everyone, I am new to fortran. – Nik Wrye Sep 14 '21 at 22:28
  • 1
    You read it once and that's fine. Then you read the _next line_ which is the start of your real numbers, but are still in that loop reading in to `firstnumber` again. That's not fine. – francescalus Sep 14 '21 at 23:01
  • So just to be clear, the first use in the first do loop is ok, but using it as a endpoint in the second do loop is not good? – Nik Wrye Sep 14 '21 at 23:12
  • @francescalus Update: I tried to use 60000 specifically but to no avail that was not the issue to the "Bad integer at unit 1 in list". I am really stuck as to how this 60000 number is a bad integer. – Nik Wrye Sep 14 '21 at 23:26
  • 2
    `60000` is not a bad integer, but you aren't reading `60000` the second time in the loop: remove that first `do`. You still won't have this program work (there are many other problems) but that first unnecessary loop is the (likely) cause of the first problem you see. – francescalus Sep 15 '21 at 05:37
  • 3
    How does `real*4` being non-standard relate to your question? It doesn't. It was a friendly unsolicited advice to a beginner that the syntax is non-standard. If it is required by your instructor, you will have to use it anyway. – Vladimir F Героям слава Sep 15 '21 at 06:00
  • 2
    Your instructor did not teach you how to debug? You will have to learn some most-basic techniques yourself - or with our help. Exactly as I wrote: Insert some `print` statements into your code so that you know what the program is doing. Let the program tell you what it has just read each time. – Vladimir F Героям слава Sep 15 '21 at 06:02
  • Yea we have not gotten to the debugging section of the course, but thank you all so much, I will apply all this information to the program. Hopefully I can get it working. – Nik Wrye Sep 16 '21 at 13:47

1 Answers1

2

To expand on francescalus' and Vladimir F's comments:

Catching bugs

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 current error

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.

The empty read

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.

In a loop

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:

  • open the file
  • read the first number in the file, but not store it anywhere
  • calculate the sum, average and standard deviation of the entire array arrayone, even though most of the elements of arrayone are not yet filled.
  • close the file

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

veryreverie
  • 2,871
  • 2
  • 13
  • 26