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!

Transferring value into a do loop

Status
Not open for further replies.

clergy1122

Technical User
Jul 2, 2008
3
CA
I'm really a novice. I wrote a small utility to calculate averages of numbers which it reads from a file. After reading I discover that it does not send the same values to the next loop. Unfortunately it works for a simple data set but not for my problem which has large data.
Here is the program;

Program Average
implicit none

integer::max
parameter(max=50)
integer::i,i1,j,N,tmp,N1,Num
real::K1,K2,K3,K4

integer,dimension(max)::Tag
real,dimension(max)::Asv,k,ar3,vol,ln,Asl,Avol,Aln

open(unit=1,file="ave2")
read(1,*)N
!write(,*)N
do i=1,N
read(1,*)Tag(i),k(i),ar3(i),vol(i),ln(i)
K1=k(i);k2=ar3(i);K3=vol(i);K4=ln(i)
enddo

Num=0
do i=1,N
K1 = k(i)
K2 = ar3(i)
K3 = vol(i)
K4 = ln(i)
N1=1
print*,K1,K2,K3,K4
print*,k(i),ar3(i),vol(i),ln(i)
do j=i+1,N
if(Tag(i).EQ.Tag(j))then
N1=N1+1
K1=K1+k(j);K2=K2+ar3(j);K3=K3+vol(j)
K4=K4+ln(j)
k(j)=0
ar3(j)=0
vol(j)=0
ln(j)=0
else
K1=K1
K2=K2
K3=K3
K4=K4
endif
enddo

Asl(i)=K1/N1
Asv(i)=K2/N1
Avol(i)=K3/N1
Aln(i)=K4/N1
! print*,Tag(i),Asl(i),Asv(i),Avol(i),Aln(i)

if(Asl(i).NE.(0.0))then
Num=Num+1
! print*,Tag(i),Asl(i),Asv(i),Avol(i),Aln(i)
! Write(3,*)Tag(i),Asl(i),Asv(i),Avol(i),Aln(i)
endif
enddo

Your help will be appreciated .

Thanks in advance
 
How much data is large data - more than 50?
 
Just a thought. Since you're reading the size, you could allocate the arrays dynamically
Code:
real,dimension(:), allocatable::Asv,k,ar3,vol,ln,Asl,Avol,Aln
...
read(1,*)N
allocate (Asv(N), k(N), ar3(N), vol(N), ln(N), Asl(N), Avol(N), Aln(N))
do i = 1,N
I don't know if ln is an intrinsic function for natural logs. Be aware of that if your program exhibits strange behaviour when you build on other platforms.
 
Neglecting the rather obvious problem -- your array is sized at 50, but you don't check to make sure N doesn't exceed that -- and walking off the end of an array in Fortran is just as easy as doing so in C, and will have equally unpredictable results.

0: When you read N make sure its value does not exceed the value of max.

1: Check the read statement's iostat result, especially for the end-of-file condition.

2: Count the number of records as they're (successfully) read.

3: Stop reading if the number of records that has been read will exceed the value of max.

4: Check to make sure that the number of records read is equal to N.

As a general piece of advice: never assume input data are totally trustworthy.

Two stylistic criticisms: don't use multiple statements on a single line, especially if they do much more than initialize something, and the series of tautological assignments (after the else statement) are superfluous, and would likely be optimized away.
 
Thanks to all that responded. Sorry that I did write back that I had seen my mistake. My actual array size is larger than 50. It had been corrected. Thanks for other corrections about my "newbie style".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top