十、数组输入输出

返回首页

用数组名来输入输出整个数组

例如:

integer A(10),W(3,2) read(*,*) A !连续输入10个整型数 read(*,*) W !按列输入 write(*,*) A,A(3),W !在一行中按列输出,不会输出换行符 write(*,100) W 100 format(1X,3I3) write(*,200) W 200 format(1X,2I3) !如果输入的W为:15 11 ! 36 49 ! 26 25 !按100格式输出为: !15 36 26 !11 49 25 !按200格式输出为: !15 36 !26 11 !49 25

利用do循环进行数组输入输出

例如:

! 每行输入一个数 do i = 1, 10 read(*,*) A(i) end do ! 输出A(1)、A(3)、A(5)、A(7)、A(9) do i = 1, 10, 2 write(*,*) A(i) end do ! 二维数组使用嵌套do循环 integer W(2,3) do 10 i=1,2 do 10 j=1,3 read(*,*) W(i,j) 10 continue

使用隐含do循环

例如:

integer A(3) A=(/1,4,2/) write(*,*) (3,A(i),i=1,3) ! 将会输出: ! 3 1 3 4 3 2

对多维数组使用隐含do循环的多层嵌套,如下:
write(*,100) ( ( W(i,j) , j=1,3 ), i=1,2 ) 100 format (1X,3F6.2)

注:1.内循环在前,用括号扩上再写外循环;
2.若无Format语句,一行输出6个元素。