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

gjleigh10
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
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.

Mentor
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

gjleigh10
Thank you so much!

berkeman
Mentor
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
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:
jedishrfu
Mentor
Let's take a closer look at your first do-loop.
Fortran:
        N = 10
open(unit = 5, file="example.dat")
do i = 1, N
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.

jedishrfu and Mark44
Mentor
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.

gmax137
Mentor
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...

Mentor
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.