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!

How to read values line by line AND count them? 1

Status
Not open for further replies.

MeteoSven

Programmer
Oct 25, 2007
7
DE
Hello!

Here's my problem:

I don't know in which way I have to change these lines, to get a program which doesn't skip into the next line after the first value that has been readed.
Any line should be read till its end and contemporary the values should be counted.

Here are the lines:

DO
READ (10, *, IOSTAT = fehler) zahl
IF (fehler == 0) THEN
summe = summe + zahl
n = n + 1
ENDIF
IF (fehler /= 0) EXIT
ENDDO

I want to read a file which looks for example like this:

3.5 7.9 30.5 21.3
2.9 3.7 2.8
3.7 4.1 3.2 19.8 210.7

(blanks between the values and no fixed format/size)


Thank you very much for any support!
 
It is a 2 stage process
Code:
character*80:: line  ! the line buffer
integer, parameter:: linemax=20 ! max on a line
real::val(linemax)
summe = 0.0
n = 0
do
   ! read a line into memory
   read (10, '(A)', end=999) line
   ! read value from the line
   read (line, *, end=888) (val(ii), ii = 1, linemax)
888 continue
   do jj = 1, ii - 1
      summe = summe + val(jj)
      n = n + 1
   enddo
enddo
999 continue
 
There is one problem left:

(val(ii), ii = 1, linemax) has the effect, that all the lines with less values are filled with the additional values of the longest one.

So I have to erase the values val(ii) after one "sum-loop"

(do jj = 1, ii - 1
summe = summe + val(jj)
n = n + 1)

but this would set all val(ii) to zero.

Is there anyway to avoid this?
 
You need to clear line first. Maybe there is a routine to do this. I'll have to check on it. The simple way is
Code:
do ii = 1, 80
   line(ii:ii) = ' '
enddo
read (...)
[CODE]
 
Is there a way to store the index ii after which the line ends, so the sum can just go ip to this one.

I tried this, but - no surprise - it doesn't work...
(Hope you understand, waht I mean)

Code:
read (line, *, end=888) (val(ii), ii = 1, linemax)
k = ii       !k is that ii where the line ends (not linemax)
888 continue
   do jj = 1, k - 1
      summe = summe + val(jj)
      n = n + 1
[CODE]
 
What is happening is that when there is no more data left, it jumps to label 888 so everything has to come after 888. This happened when it was reading val(ii) so ii-1 was the last valid one. The code should be
Code:
read (line, *, end=888) (val(ii), ii = 1, linemax)
! we have actually read linemax values so increment by 1
! so that it is correct when we subtract 1
   ii = linemax + 1
888 continue
   k = ii       !k is that ii where the line ends (not linemax)
   do jj = 1, k - 1
      summe = summe + val(jj)
      n = n + 1
 
Or just read it here... ;)

PROGRAM mittelwert

! Variablen-Deklaration
IMPLICIT NONE
REAL :: zahl, summe, mittw
CHARACTER (LEN = 128) :: datnam
INTEGER :: i, n, fehler
integer :: ii, jj, k

character*80 :: line ! the line buffer
integer, parameter :: linemax=50 ! max on a line
real :: val(linemax)

! Initialisierung
n = 0
summe = 0.0

! Startmeldung
call system ('cls')
PRINT *
PRINT *, 'Dieses Programm liest Zahlen aus einer Datei ein'
PRINT *, 'und berechnet anschliessend den Mittelwert.'
PRINT *

! Beginn Schleife - Datei oeffnen
DO
! Eingabe des Dateinamens
WRITE (*, '("Geben Sie den Dateinamen ein: ")', advance='no')
READ (*, '(A)') datnam

! Datei oeffnen
OPEN (10, FILE = datnam, STATUS = 'OLD', IOSTAT = fehler)

! Fehlermeldung, wenn Datei nicht existiert
IF (fehler /= 0) PRINT *, 'Datei existiert nicht!'

IF (fehler == 0) EXIT
END DO
! Ende Schleife - Datei oeffnen

! Datei bis zum Ende lesen und Summe berechnen


DO

! read a line into memory
read (10, '(A)', end=999) line

do k=1,linemax,1
val(k)=-1E12
end do

! read value from the line
read (line, *, end=888) (val(ii), ii = 1, linemax)
888 continue

do jj = 1, ii-1
if (val(jj).ne.-1E12) summe = summe + val(jj)
if (val(jj).ne.-1E12) n = n + 1 ! Alte Werte werden nicht mitgezaehlt
end do

END DO


999 continue

! Datei schliessen
CLOSE (10)

! Mittelwert berechnen
mittw = summe / n

! Ausgabe der Ergebnisse
PRINT *
WRITE(*, '("Anzahl der gelesenen Zahlen = ", i3)') n
WRITE(*, '(" Summe = ", f8.2)') summe
WRITE(*, '(" Mittelwert = ", f8.2)') mittw

PRINT *

END

! ---------------------------------------------------------------------------

 
Think you should put all your comments in German otherwise it looks a bit odd.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top