Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding the average of a column in an array

Status
Not open for further replies.

WSUPhys

Technical User
Feb 6, 2013
20
US
I have a program which reads a .csv file into an array and stores the values of the different variables into columns. I am trying to find the average value of the 2nd, 3rd and 4th columns (ivar1,ivar2,ivar3). I have the following code {this is just the loop to calculate the sum of the values and their average:
Code:
Do 11 k=1,n
 Sum(U)=Sum(U)+((ABS(ivar(k)))
 AVGFor=((Sum(U)/1000)/n)
11 continue
Write(*,*) 'AVGFor'
the sum of the columns are divided by 1000 because of a scaling factor and then by the value k which is the total number of lines read.

This seems like it should work but when I try to compile I return errors, could somebody give me some advice on this?

The rest of the code is here:
Code:
program rd
    implicit none
    integer stat, cmd_rc, num_lines, ivar1, ivar2, ivar3,u,k,n,AVGFor
    real :: rvar
    integer i
    character*100 line
    
    do i=1,10
        read(*,*) line
    end do
    
    num_lines = 0
    do
      read(*,*,end=99, iostat=stat) rvar, ivar1, ivar2, ivar3
      if (stat .ne. 0) then
        write(*,*) 'Error reading data !'
        exit
      end if
      num_lines = num_lines + 1
      if (num_lines <=  10) then
        write(*,*) num_lines, ':', rvar, ivar1, ivar2, ivar3
      end if
    end do
    99 continue
  [COLOR=#3465A4]  Do 11 k=1,n
      Sum(U)=Sum(U)+((ABS(ivar(k)))
      AVGFor=((Sum(U)/1000)/n)
11  continue
    Write(*,*) 'AVGFor'[/color]
    
    write(*,*) '...done.'
    write(*,*) 'Lines processed: ', num_lines    
end program rd

 
You don't need arrays to compute averages. You can compute sums in the same DO-LOOP where you read the data from the file and at the end comute the averages - like this:
Code:
    integer stat, cmd_rc, num_lines, ivar1, ivar2, ivar3, &
            [highlight]Sum_ivar1, Sum_ivar2, Sum_ivar3, Avg_ivar1, Avg_ivar2, Avg_ivar3[/highlight]
    ...
    ...
    num_lines = 0
    [highlight]
    Sum_ivar1 = 0
    Sum_ivar1 = 0
    Sum_ivar1 = 0[/highlight]
    do
      read(*,*,end=99, iostat=stat) rvar, ivar1, ivar2, ivar3
      if (stat .ne. 0) then
        write(*,*) 'Error reading data !'
        exit
      end if
      num_lines = num_lines + 1
      if (num_lines <=  10) then
        write(*,*) num_lines, ':', rvar, ivar1, ivar2, ivar3
      end if
      [highlight]
      Sum_ivar1 = Sum_ivar1 + ivar1 
      Sum_ivar2 = Sum_ivar2 + ivar2 
      Sum_ivar3 = Sum_ivar3 + ivar3 [/highlight]
    end do
    99 continue
    ...
    [highlight]
    Avg_ivar1 = Sum_ivar1 / num_lines
    Avg_ivar2 = Sum_ivar2 / num_lines
    Avg_ivar3 = Sum_ivar3 / num_lines[/highlight]
    ...
    ...
 
thank you, I just do not have enough practice dealing with certain elements of Fortran and so was trying to do stuff based on the brute force method I learned years ago.
 
I want the absolute value of the variables because the numbers in my csv file are both positive and negative (Link so on the sum would I just put
Code:
Sum_ivar1 = Sum_ivar1 + ABS(ivar1)
? Will this work or is there something different I need to do?
 
WSUPhys said:
I just do not have enough practice dealing with certain elements of Fortran and so was trying to do stuff based on the brute force method
You probably mean with brute force this
Code:
    Do 11 k=1,n
      Sum(U)=Sum(U)+((ABS(ivar(k)))
      AVGFor=((Sum(U)/1000)/n)
11  continue
    Write(*,*) 'AVGFor'
But this will not work in this program because we don't have defined the array U and the array ivar. In this program ivar1, ivar2, ivar3 are not arrays, but only integer variables. It would be stupid to store the columns in the arrays, when you only need to compute averages - imagine that every array would need 72000 elements.

I want the absolute value of the variables because the numbers in my csv file are both positive and negative ...
Try as you suggested above and see if it delivers reasonable results as you expected i.e.:
Code:
Sum_ivar1 = Sum_ivar1 + ABS(ivar1)
Or maybe something like this
Code:
Sum_ivar1 = Sum_ivar1 + ivar1 
...
Avg_ivar1 = ABS(Sum_ivar1) / num_lines


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top