I got an email, that juice34x has responsed to his/her thread, but I don't see nothing (was the thread deleted?).
I remember, the task was about reading data from the text file into array and then process the array, for example compute the average.
So, I wrote a little program to demonstrate, how to solve it, but the primary thread is no more available. Therefore I post the code here:
Given is a data file, e.g. inpt.dat
Here is the program read_data.for
Compilation (g77 with MSYS on Windows):
Running the program
I remember, the task was about reading data from the text file into array and then process the array, for example compute the average.
So, I wrote a little program to demonstrate, how to solve it, but the primary thread is no more available. Therefore I post the code here:
Given is a data file, e.g. inpt.dat
Code:
30
50
10
80
40
20
60
Here is the program read_data.for
Code:
program read_data
implicit none
character(30) :: fname
integer :: i,nr_elements,stat,nr_lt_average,nr_gt_average
real :: x(200), sum_of_array, average
10 write(*,*) 'Enter input file name: '
read(*,*) fname
open(1,file=fname,status='old',iostat=stat)
if (stat .ne. 0) then
write(*,*) fname, ' cannot be opened !'
go to 10
end if
nr_elements = 0
do i = 1, 200
read(1,*,end=20,err=30) x(i)
nr_elements = nr_elements + 1
end do
20 close(1)
! print reading results
write(*,*) 'number of data elements in the file =', nr_elements
write(*,*) 'Array='
call print_array(x, nr_elements)
! compute average
average = sum_of_array(x, nr_elements)/nr_elements
write(*,*) 'Average =', average
! number of less and greater elements than average
nr_lt_average = 0
nr_gt_average = 0
do i =1, nr_elements
if (x(i) .lt. average) then
nr_lt_average = nr_lt_average + 1
else
if (x(i) .gt. average) then
nr_gt_average = nr_gt_average + 1
end if
end if
end do
write(*,*) 'Number of Elements less than Average =', nr_lt_average
write(*,*) 'Number of Elements greater than Average =', nr_gt_average
!!!
stop
30 write(*,*)'I/O error reading file !'
end program read_data
! ************** Functions/Procedures **************
subroutine print_array(array, n)
implicit none
integer :: n, i
real :: array(n)
do i = 1, n
print *, array(i)
end do
end subroutine print_array
real function sum_of_array(array, n)
implicit none
integer :: n, i
real :: array(n)
sum_of_array = 0.
do i =1, n
sum_of_array = sum_of_array + array(i)
end do
end function sum_of_array
Compilation (g77 with MSYS on Windows):
Code:
$ g77 read_data.for -o read_data -ffree-form
Running the program
Code:
$ read_data
Enter input file name:
inpt
inpt cannot be opened !
Enter input file name:
inpt.dat
number of data elements in the file = 7
Array=
30.
50.
10.
80.
40.
20.
60.
Average = 41.4285698
Number of Elements less than Average = 4
Number of Elements greater than Average = 3