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.
 

1. How do I read a single column of data into Fortran without using arrays?

To read a single column of data into Fortran without using arrays, you can use the READ statement followed by the variable name for each data point. For example, if you have a column of integers in a file called "data.txt", you can use the following code:

OPEN(1,FILE='data.txt')

DO I=1,10

READ(1,*) A

END DO

This will read each line of the file and assign the value to the variable "A".

2. Can I read multiple columns of data into Fortran without using arrays?

Yes, you can read multiple columns of data into Fortran without using arrays by using the READ statement multiple times, each time assigning the value to a different variable. For example, if you have two columns of integers in a file called "data.txt", you can use the following code:

OPEN(1,FILE='data.txt')

DO I=1,10

READ(1,*) A, B

END DO

This will read each line of the file and assign the first value to the variable "A" and the second value to the variable "B".

3. How do I skip over certain columns of data when reading into Fortran without using arrays?

To skip over certain columns of data when reading into Fortran without using arrays, you can use the * character in the READ statement. This will tell Fortran to read the data but not assign it to a variable. For example, if you have a file with three columns of data and you only want to read the first and third columns, you can use the following code:

OPEN(1,FILE='data.txt')

DO I=1,10

READ(1,*) A, *, B

END DO

This will read the first and third columns of data but skip over the second column.

4. How do I handle missing data when reading into Fortran without using arrays?

To handle missing data when reading into Fortran without using arrays, you can use the IOSTAT keyword in the READ statement. This will return a value indicating whether the read was successful or not. If the read is not successful, you can use an IF statement to handle the missing data. For example, if you have a file with two columns of data and some missing values, you can use the following code:

OPEN(1,FILE='data.txt')

DO I=1,10

READ(1,*,IOSTAT=IERR) A, B

IF (IERR .EQ. 0) THEN

! handle data

ELSE

! handle missing data

END IF

END DO

5. Can I read data from a file into a specific location in memory without using arrays in Fortran?

Yes, you can use the LOC keyword in the READ statement to specify the memory location where you want to read the data into. For example, if you have a file with two columns of data and you want to read the data into a specific location in memory, you can use the following code:

OPEN(1,FILE='data.txt')

DO I=1,10

READ(1,*,LOC=A) B

END DO

This will read the data from the file into the memory location specified by the variable "A".

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top