fortranfortran90

how to correctly record the counting of averages between the minimum and maximum of the matrix on fortran


I was trying to write a program to calculate the average without considering the minimum and average of the columns, what is the best way to do this? To begin with, I wanted to simply output the average value of the columns to a file, there are no errors, but nothing is output.

Program Matrix
 Implicit None
Real,Allocatable,dimension(:,:)::A 
Real,Allocatable,dimension(:):: b 
Integer varStr,varStlb 
Integer i, j  
real summa
Open(1,file='in.txt') 
Open(8,file='out.txt') 
Do 
read(*,*)varStr,varStlb
Allocate(A(1:varStr,1:varStlb),B(1:varStlb)) 
Read(1,*) ( A(i,:), i = 1,varStr )
do j = 1, varStlb           
   summa = 0
   do i = 1, varStr
     summa = summa + a(j,i) 
   end do
   b(j) = summa/varStr       
   write(8,'(A,F8.2,A)')'b = ',b(j), ' - сумма всех элементов' 
 end do 
Deallocate(A)
Enddo
End Program Matrix

in file is

10.05   -22.0   3.0
4.0 0.0 60.0
8.0 13.0    22.5 

Solution

  • As I wrote, there are too many points in your question. Therefore it is hard to make an answer, that covers your question well and does not give you just a solution to your homework without real understanding of the problem.

    You have many problems or strange points in your code

    1. I do not see the reason for the outer Do loop. In this loop you will try to read from the file many times. There is no protection to read past the end of the file.

    2. When reading from the file you always allocate your arrays. But you never deallocate them In the next iteration of the outer Do loop you try to allocate them again, but that is not allowed, they are allocated already.
      Perhaps you just wanted to read the whole file just once?

    3. I always suggest to learn in steps. First read the file as you need it, print it, verify that it is in the form you needed. Only than compute normal averages. Only then compute averages that exclude maximum and minimum.

    4. Ignoring the maximum and the minimum is simple, just subtract them from the sum and decrease the count of the elements by 2. However, as the rest of your code is so chaotic, it is impossible to just add it there. I will just show a sketch of the procedure:

    Normal average of array a(1:n):

    avg = sum(a) / n
    

    Average of all elements except the maximum and the minimum:

    avg2 = (sum(a) - minval(a) - maxval(a)) / (n - 2)