How to read a column of data into Fortran without arrays? (Fortran 77)

In summary: N write(8,*) sum = sum + i end do close(8) end program
  • #1
gjleigh10
2
1
Thread moved from the technical forums to the schoolwork forums
TL;DR Summary: I am beginning research as an undergrad in Physics and have an example file I must analyze for the mean of each column. I cannot use arrays. I need to take the sum of a column of data.

Hi all, I am new to fortran and programming in general, but I'm having issues with creating a code which will help me find the average of a column without using arrays. I am using an example data file to read the information into my program, and I want to take the sum of the first column. However, I am having a hard time finding out how to do this without arrays (especially when there is more than one column to sum individually.) Here is my data file:
Code:
54
21
32
67
76
82
12
65
39
26

And here is what I have written so far:
Fortran:
program mean_analysis
        implicit none
        
        integer i
        integer N
        integer sum
        integer a 
        sum = 0
        
        
        N = 10
        open(unit = 5, file="example.dat")
            do i = 1, N 
                read(5,*) a
            end do
        close(5)
        
        open(unit = 8, file="outputanalysis.dat")
            do i = 1, N
                write(8,*) sum = sum + i
            end do
        close(8)
        
        end program

I am trying to make "a" the first column of data to read. I don't know if I did this correctly. Compiling this program gives me an error: 20 | write(8,*) sum = sum + i | 1 Error: Syntax error in WRITE statement at (1) I am trying to take the sum of every number in the column. Can someone point me in the right direction? Thank you very much.
 
Physics news on Phys.org
  • #2
In your first loop where you read data into you could add a second line to do

sum=sum+a

then you only need a single write statement to print out the average

write(8,*) sum/N
 
  • #3
Thank you so much!
 
  • Like
Likes berkeman
  • #4
gjleigh10 said:
Hi all, I am new to fortran and programming in general, but I'm having issues with creating a code which will help me find the average of a column without using arrays.
A natural, but incorrect inclination in new programmers is thinking that when your program needs to read a list of numbers, the program must use an array to do so. As @jedishrfu pointed out, all you need to do is keep a running total by accumulating the sum at each iteration of the read loop.

The reason you got the compiler error is that your write statement write(8,*) sum = sum + a included an executable statement (i.e., sum = sum + a), rather than an expression whose value is to be written.

Overall, your program looked pretty good, but I would recommend that you indent only the body of a do loop rather than the entire do loop. The main purpose of indentation is to indicate visually the flow of the program. Lines of code that are indented the same execute the same number of times. In the code below, the two assignment statements execute once apiece, as does the do loop itself (the code inside the do loop executes 10 times, though).
Here's how I would write the do loop, also incorporating the accumulating sum.
Fortran:
        sum = 0
        N = 10

        open(unit = 5, file="example.dat")
        do i = 1, N
           read(5,*) a
           sum = sum + a
        end do
        close(5)
Note that if you didn't know a priori how many numbers the file would contain, there are several techniques that can be used so that the program "knows" when to quit reading.
After the close statement exits, sum will contain the sum of the numbers in the file. You can then display the average of the numbers in the file with a single write statement.

Fortran:
     write(*, *) "Average: ", sum/N

Or write it to an output file like in your program.
Fortran:
      open(unit = 8, file="outputanalysis.dat")
      write(*, *) "Average: ", sum/N
      close(8)
 
Last edited:
  • Like
Likes jedishrfu
  • #5
Let's take a closer look at your first do-loop.
gjleigh10 said:
Fortran:
        N = 10
        open(unit = 5, file="example.dat")
            do i = 1, N
                read(5,*) a
            end do
        close(5)
I am trying to make "a" the first column of data to read.
The variable a contains a single integer. It cannot contain an entire column at once. For that, it would have to be an array.

The first time around the loop (i = 1), the program reads the first number in the file and stores it in a. The second time around the loop (i = 2), the program reads the second number and stores it in a, overwriting (discarding) the first number. The third time around the loop (i = 3), the program reads the third number and stores it in a, overwriting (discarding) the second number. And so on.

When the loop finishes, a contains the tenth number from the file.
 
  • Like
Likes jedishrfu and Mark44
  • #6
As @Mark44 mentioned there are several ways to indicate the end of your list of numbers:
- put a number card at the front of your data to read in and to control your input loop
- add a sentinel card at the end of your data like a -99999 card so when you read -99999 your know you’ve reached the end of the input data

with respect to the sentinel card, there’s a funny computer error story. A programmer submitted a batch job to the computer. The job read in numbers from a card reader until it saw the sentinel card. The next day he’d get back his output only to discover it had failed while reading the cards.

Finally, he decided to observe the job while it was running and to his chagrin saw a floor sweeper borrow the top card from the card reader stack ie his input data to use for picking up his sweepings. That card was the sentinel card as the card deck is placed facedown in the gravity fed card reader hopper.
 
  • Wow
Likes gmax137
  • #7
jedishrfu said:
As @Mark44 mentioned there are several ways to indicate the end of your list of numbers:
- put a number card at the front of your data to read in and to control your input loop
- add a sentinel card at the end of your data like a -99999 card so when you read -99999 your know you’ve reached the end of the input data
I'm close to 100% certain that the OP's program isn't going to be reading a stack of cards...
 
  • #8
jtbell said:
The variable a contains a single integer. It cannot contain an entire column at once. For that, it would have to be an array.
Yes. To declare an array that can hold 10 integers, a program would have a declaration like one of the following:
Code:
integer, dimension(10) :: a
or
Code:
integer a(10)

There are a number of ways to declare an an array, mostly because Fortran has been around for a long time, and there are different standards, such as Fortran 77, Fortran 90, Fortran 95, and so on.
It should emphasized that for the posted problem, an array is not necessary, and should not be used.
 

FAQ: How to read a column of data into Fortran without arrays? (Fortran 77)

How can I read a column of data without using arrays in Fortran 77?

In Fortran 77, you can read a column of data without using arrays by reading each data point individually within a loop. You can use a DO loop to iterate through each line of the input file and process the data accordingly.

What is the basic structure of a Fortran 77 program to read data without arrays?

A basic Fortran 77 program to read data without arrays involves opening the file, using a DO loop to read each line, and then processing the data within the loop. Here is a simple example:

      PROGRAM ReadData      INTEGER I, N      REAL X      OPEN(UNIT=10, FILE='data.txt', STATUS='OLD')      DO 10 I = 1, N         READ(10, *) X         ! Process the data point X here    10 CONTINUE      CLOSE(10)      END

How do I handle end-of-file (EOF) when reading data in Fortran 77?

To handle the end-of-file condition in Fortran 77, you can use the IOSTAT specifier in the READ statement. This allows you to check if the end of the file has been reached and exit the loop accordingly. Here is an example:

      PROGRAM ReadData      INTEGER I, N, IOStatus      REAL X      OPEN(UNIT=10, FILE='data.txt', STATUS='OLD')      I = 0      DO         READ(10, *, IOSTAT=IOStatus) X         IF (IOStatus /= 0) EXIT         I = I + 1         ! Process the data point X here      END DO      CLOSE(10)      END

Can I read formatted data without using arrays in Fortran 77?

Yes, you can read formatted data without using arrays in Fortran 77 by specifying the appropriate format in the READ statement. For example, if your data is in a fixed format, you can use a format specifier to read each data point:

      PROGRAM ReadData      INTEGER I, N      REAL X      OPEN(UNIT=10, FILE='data.txt', STATUS='OLD')      DO 10 I = 1, N         READ(10, '(F10.2)') X         ! Process the data point X here    10 CONTINUE      CLOSE(10)      END

How do I handle different data types when reading a column of data in Fortran 77?

Similar threads

Replies
13
Views
3K
Replies
2
Views
6K
Replies
12
Views
3K
Replies
21
Views
2K
Replies
3
Views
4K
Replies
1
Views
11K
Replies
7
Views
4K

Back
Top