fortran中二维数组的声明、输入与输出

 

001、reshape

fortran中二维数组的声明、输入与输出

root@ubuntu26:/home/test01/fortran# ls
test01.f90
root@ubuntu26:/home/test01/fortran# cat test01.f90
program test01_fortran
    implicit none

    integer :: ay1(2,3) = reshape([11,22,33,44,55,66], [2,3])       ! reshape输入元素数值是按照列进行的
    integer :: i,j

    do i = 1,2
        do j = 1,3
            write(*, "(I4)", advance="no") ay1(i,j)
        end do
        print *
    end do

end program test01_fortran
root@ubuntu26:/home/test01/fortran# gfortran test01.f90 -Wall -O2 -o kkk          ## 编译
root@ubuntu26:/home/test01/fortran# ls
kkk  test01.f90
root@ubuntu26:/home/test01/fortran# ./kkk
  11  33  55
  22  44  66

 

image

 。

 

002、键盘输入

root@ubuntu26:/home/test01/fortran# ls
test01.f90
croot@ubuntu26:/home/test01/fortran# cat test01.f90
program test01_fortran
    implicit none
    integer :: i,j
    integer :: ay1(2,3)                               ! 声明二维数组
    do i = 1,2
        do j = 1,3
            write(*, "(A, I1, A, I1, A)", advance="no") "ay1(", i,",", j, "):"        ! 输入二维数组
            read *, ay1(i,j); end do
    end do
    do i = 1, 2
        do j = 1, 3
            write(*, "(I4)", advance="no") ay1(i,j)                         ! 输出二维数组
        end do; print *
    end do
end program test01_fortran
root@ubuntu26:/home/test01/fortran# gfortran test01.f90 -Wall -O2 -o kkk
root@ubuntu26:/home/test01/fortran# ls
kkk  test01.f90
root@ubuntu26:/home/test01/fortran# ./kkk
ay1(1,1):11
ay1(1,2):22
ay1(1,3):33
ay1(2,1):44
ay1(2,2):55
ay1(2,3):66
  11  22  33
  44  55  66

image

 

。

 

posted @ 2026-10-26 14:51  小鲨鱼2018  阅读(2)  评论(0)    收藏  举报