Variable format

﹥>﹥吖頭↗ 提交于 2019-11-27 08:37:18

问题


I wrote a program to calculate a square finite difference matrix, where you can enter the number of rows (equals the number of columns) -> this is stored in the variable matrix. The program works fine:

program fin_diff_matrix

  implicit none

  integer, dimension(:,:), allocatable :: A     
  integer :: matrix,i,j

  print *,'Enter elements:'
  read *, matrix

  allocate(A(matrix,matrix))

  A = 0
  A(1,1) = 2
  A(1,2) = -1
  A(matrix,matrix) = 2
  A(matrix,matrix-1) = -1

  do j=2,matrix-1
    A(j,j-1) = -1
    A(j,j) = 2
    A(j,j+1) = -1
  end do

  print *, 'Matrix A: '
  write(*,1) A

  1 format(6i10)

end program fin_diff_matrix

For the output I want that matrix is formatted for the output, e.g. if the user enters 6 rows the output should also look like:

         2        -1         0         0         0         0
        -1         2        -1         0         0         0
         0        -1         2        -1         0         0
         0         0        -1         2        -1         0
         0         0         0        -1         2        -1
         0         0         0         0        -1         2

The output of the format should also be variable, e.g. if the user enters 10, the output should also be formatted in 10 columns. Research on the Internet gave the following solution for the format statement with angle brackets:

  1 format(<matrix>i<10)

If I compile with gfortran in Linux I always get the following error in the terminal:

       fin_diff_matrix.f95:37.12:

     1 format(<matrix>i10)
            1
   Error: Unexpected element '<' in format string at (1)
   fin_diff_matrix.f95:35.11:

     write(*,1) A
           1
   Error: FORMAT label 1 at (1) not defined

What doesn't that work and what is my mistake?


回答1:


The syntax you are trying to use is non-standard, it works only in some compilers and I discourage using it.

Also, forget the FORMAT() statements for good, they are obsolete.

You can get your own number inside the format string when you construct it yourself from several parts

character(80) :: form
form = '(          (i10,1x))'
write(form(2:11),'(i10)') matrix

write(*,form) A

You can also write your matrix in a loop per row and then you can use an arbitrarily large count number or a * in Fortran 2008.

do i = 1, matrix
  write(*,'(999(i10,1x))') A(:,i)
end do

do i = 1, matrix
  write(*,'(*(i10,1x))') A
end do

Just check if I did not transpose the matrix inadvertently.



来源:https://stackoverflow.com/questions/29558485/variable-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!