# Fortran數組

Numbers(1)

Numbers(2)

Numbers(3)

Numbers(4)

## 聲明數組

real, dimension(5) :: numbers

integer, dimension (5,5) :: matrix

real, dimension(2:6) :: numbers
integer, dimension (-3:2,0:4) :: matrix

## 賦值

numbers(1) = 2.0

do i=1,5 numbers(i) = i * 2.0 end do

numbers = (/1.5, 3.2,4.5,0.9,7.2 /)

program arrayProg

real :: numbers(5) !one dimensional integer array
integer :: matrix(3,3), i , j !two dimensional real array !assigning some values to the array numbers do i=1,5 numbers(i) = i * 2.0 end do !display the values do i = 1, 5 Print *, numbers(i) end do !assigning some values to the array matrix do i=1,3 do j = 1, 3 matrix(i, j) = i+j end do end do !display the values do i=1,3 do j = 1, 3 Print *, matrix(i,j) end do end do !short hand assignment
numbers = (/1.5, 3.2,4.5,0.9,7.2 /) !display the values do i = 1, 5 Print *, numbers(i) end do end program arrayProg

2.00000000
4.00000000
6.00000000
8.00000000
10.0000000
2
3
4
3
4
5
4
5
6
1.50000000
3.20000005
4.50000000
0.899999976
7.19999981

Rank

Extent

Shape

Size

## 數組傳遞給過程

program arrayToProcedure implicit none

integer, dimension (5) :: myArray
integer :: i

call fillArray (myArray) call printArray(myArray) end program arrayToProcedure

subroutine fillArray (a) implicit none

integer, dimension (5), intent (out) :: a ! local variables
integer :: i do i = 1, 5 a(i) = i end do end subroutine fillArray

subroutine printArray(a) integer, dimension (5) :: a
integer::i do i = 1, 5 Print *, a(i) end do end subroutine printArray

1
2
3
4
5

program arrayToProcedure implicit none

integer, dimension (10) :: myArray
integer :: i interface subroutine fillArray (a) integer, dimension(:), intent (out) :: a
integer :: i end subroutine fillArray

``````  subroutine printArray (a) integer, dimension(:) :: a
integer :: i end subroutine printArray end interface call fillArray (myArray) call printArray(myArray) end program arrayToProcedure``````

subroutine fillArray (a) implicit none
integer,dimension (:), intent (out) :: a ! local variables
integer :: i, arraySize
arraySize = size(a) do i = 1, arraySize
a(i) = i end do end subroutine fillArray

subroutine printArray(a) implicit none

integer,dimension (:) :: a
integer::i, arraySize
arraySize = size(a) do i = 1, arraySize Print *, a(i) end do end subroutine printArray

1
2
3
4
5
6
7
8
9
10

## 數組部分

array ([lower]:[upper][:stride], ...)

program arraySubsection

real, dimension(10) :: a, b
integer:: i, asize, bsize

a(1:7) = 5.0 ! a(1) to a(7) assigned 5.0 a(8:) = 0.0 ! rest are 0.0 b(2:10:2) = 3.9 b(1:9:2) = 2.5 !display
asize = size(a) bsize = size(b) do i = 1, asize Print *, a(i) end do do i = 1, bsize Print *, b(i) end do end program arraySubsection

5.00000000
5.00000000
5.00000000
5.00000000
5.00000000
5.00000000
5.00000000
0.00000000E+00
0.00000000E+00
0.00000000E+00
2.50000000
3.90000010
2.50000000
3.90000010
2.50000000
3.90000010
2.50000000
3.90000010
2.50000000
3.90000010

## 數組內部函數

FORTRAN 90/95提供了一些固有的程序。它們可分爲7類。

• 向量和矩陣乘法

• 還原

• 查詢

• 結構

• 重塑

• 處理

• 位置