# 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 它的尺寸陣列具有的數目。例如，對於陣列命名矩陣，秩為2，並且對陣列命名號，秩為1。
Extent 它是沿一維中的元素的數量。例如，陣列數位具有範圍5和命名矩陣陣列具有在兩個維度範圍3。
Shape 陣列的形狀是一維整數陣列，包含在每一維的元素（程度）的數量。例如，對於陣列基質，形狀為（3,3）和陣列數位它是（5）。
Size 它是元素的陣列中包含的數量。對於陣列矩陣，它是9，而對於數位陣列，其為5。

## 陣列傳遞給過程

```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類。