Creating an Array: Difference between revisions
Content added Content deleted
m (→{{header|Fortran}}: lang tags) |
(→{{header|Fortran}}: removed initializator (to array initialization), added some details (inspection too; maybe better somewhere else?)) |
||
Line 276: | Line 276: | ||
will be an two-dimensional, 5x12-array of type REAL, where the first dimension can be addressed numerically as 25, 26, 27, 28 or 29 (and the second dimension as 1 .. 12). |
will be an two-dimensional, 5x12-array of type REAL, where the first dimension can be addressed numerically as 25, 26, 27, 28 or 29 (and the second dimension as 1 .. 12). |
||
Fortran 90 and later can use also the syntax |
|||
In ISO Fortran 95 or later, arrays can also be initialized using a robust set of array constructors and array intrinsic functions. |
|||
<lang fortran> program arrays |
|||
real :: a(100) = 0 ! entire array initialized to ZERO |
|||
real :: b(9) = (/ 1,2,3,4,5,6,7,8,9 /) ! array constructor |
|||
real :: c(10) = (/ (2**i, i=1, 10) /) ! array constructor with "implied do loop" |
|||
! An array constructor is a literal definition of a ONE-DIMENSIONAL array only. |
|||
! In order to initialize multidimensional arrays, you need to use the RESHAPE instrinsic |
|||
! function: |
|||
real :: d(2, 5) = reshape( (/ (2**i,i=1,10) /), (/ 2, 5 /) ) |
|||
! ^^^^^^^^^^^^^^^^^^^source array ^^^^^^^^^^ dimension shape |
|||
! Fills the array in COLUMN MAJOR order. That is, traverse the first dimension first, |
|||
! second dimension second, etc. |
|||
! In order to fill a matrix in ROW MAJOR order, merely specify as before, |
|||
! but TRANSPOSE the result |
|||
real :: e(4, 4) = transpose( reshape( & |
|||
(/ 1, 2, 3, 4, & |
|||
5, 6, 7, 8, & |
|||
9, 10, 11, 12, & |
|||
13, 14, 15, 16 /), (/ 4, 4 /) ) ) |
|||
print *, b |
|||
print * |
|||
print *, c |
|||
print * |
|||
print '(5F7.0)', d(1,:) ! print row 1 |
|||
print '(5F7.0)', d(2,:) ! print row 2 |
|||
print * |
|||
do i = 1, 4 |
|||
print '(4F7.0)', e(i,:) ! print row I |
|||
end do |
|||
end program arrays</lang> |
|||
<lang fortran>real, dimension(20) :: array</lang> |
|||
Output: |
|||
1. 2. 3. 4. 5. 6. 7. 8. 9. |
|||
2. 4. 8. 16. 32. 64. 128. 256. 512. 1024. |
|||
2. 8. 32. 128. 512. |
|||
4. 16. 64. 256. 1024. |
|||
1. 2. 3. 4. |
|||
5. 6. 7. 8. |
|||
9. 10. 11. 12. |
|||
13. 14. 15. 16. |
|||
Dynamic array (in Fortran 90 and later) can be declared as: |
|||
Here is a more interesting example showing a function that creates and returns a square identity matrix of order N: |
|||
<lang fortran> module matrixUtil |
|||
contains |
|||
function identity(n) |
|||
integer, intent(in) :: n |
|||
integer :: i, j |
|||
real, pointer :: identity(:,:) |
|||
allocate(identity(n,n)) |
|||
! the MERGE intrinsic is like a C ternary (if-then-else) operator, except that the logical condition |
|||
! comes at the end, it is a logical ARRAY, and the function result is an array of the same size and shape |
|||
identity = merge (1.0, 0.0, reshape( (/ ( (i==j, i=1,n), j=1,n) /), (/ n, n /) ) ) |
|||
! ^^^ if-TRUE-value ^^^ if-FALSE-value ^^<-NxN logical array, true only on the diagonal->^^ |
|||
! The TVALUE and FVALUE must be "conformable" to the logical array, meaning that each is either: |
|||
! -- an array of the same size and shape as the logical array |
|||
! -- a scalar |
|||
end function identity |
|||
end module matrixUtil</lang> |
|||
<lang fortran> |
<lang fortran>integer, dimension(:), allocatable :: array |
||
real, dimension(:,:), allocatable :: multidim_array</lang> |
|||
use matrixUtil |
|||
integer :: n |
|||
real, pointer :: i(:,:) |
|||
read (*,*) n |
|||
i => identity(n) |
|||
do j=1, size(i,1) |
|||
print *, i(j,:) |
|||
end do |
|||
deallocate(i) |
|||
end program muTest</lang> |
|||
and then the space can be allocated/deallocated with: |
|||
Example run: |
|||
$ <span style="color: blue;">g95 -o mu matrixUtil.f95 muTest.f95</span> |
|||
<lang fortran>allocate(array(array_dimension)) |
|||
$ <span style="color: blue;">./mu</span> |
|||
allocate(multidim_array(20,20)) |
|||
<span style="color: red;">15</span> |
|||
!... |
|||
1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
deallocate(array) |
|||
0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
deallocate(multidim_array)</lang> |
|||
0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
where <tt>array_dimension</tt> is an integer. |
|||
0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
Dimension of array can be inspected with <tt>size</tt> intrinsic, and bounds with <tt>ubound</tt> and <tt>lbound</tt>: |
|||
0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. |
|||
<lang fortran> real :: array(20:30), another(10,0:19) |
|||
0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. |
|||
print *, size(another) ! 10 elements for the first "column", 20 for the second, |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. |
|||
! we obtain 200 |
|||
print *, size(array) ! from 20 to 30, we can hold 11 values |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. |
|||
print *, size(another, 1) ! "column" 1 has 10 elements, from 1 to 10 |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. |
|||
print *, size(another, 2) ! "column" 2 has 20 elements, from 0 to 19 |
|||
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. |
|||
print *, lbound(array) ! lower bound is 20 |
|||
$ |
|||
print *, ubound(array) ! upper bound is 30 |
|||
print *, lbound(another,1) ! is 1 |
|||
print *, lbound(another,2) ! is 0 |
|||
print *, lbound(another) ! return an array, (/ 1, 0 /) |
|||
print *, ubound(another,1) ! is 10 |
|||
print *, ubound(another,2) ! is 19 |
|||
print *, ubound(another) ! return an array, (/ 10, 19 /) </lang> |
|||
Outputs |
|||
<pre> 200 |
|||
11 |
|||
10 |
|||
20 |
|||
20 |
|||
30 |
|||
1 |
|||
0 |
|||
1 0 |
|||
10 |
|||
19 |
|||
10 19</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |