# 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 ```