fortran中二维数组的输入与输入;数组列与行的和

 

001、

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

    integer :: i,j,total_val
    integer :: ay1(2,3)
    do i = 1,2
        write(*, "(A, I0, A)", advance="no") "input row ", i, ": "; read *, ay1(i,:)       !按照行写入数组
    end do
    do i = 1,2; print *, ay1(i,:); end do                                         ! 按照行输出数组
    do i = 1,2; total_val = sum(ay1(i,:)); print *, "row", i, "total: ", total_val; end do    ! 输出二维数组每行的和
    do j = 1,3; total_val = sum(ay1(:,j)); print *, "column ", j, " total: ", total_val; 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       ## 测试程序
input row 1: 10 20 30
input row 2: 60 40 50
          10          20          30
          60          40          50
 row           1 total:           60
 row           2 total:          150
 column            1  total:           70
 column            2  total:           60
 column            3  total:           80

 

image

 

。

 

posted @ 2026-10-29 22:47  小鲨鱼2018  阅读(6)  评论(0)    收藏  举报