Array Initialization: Difference between revisions

fortran (code moved from "creating an array")
(Added D examples for array init)
(fortran (code moved from "creating an array"))
Line 87:
y["world"] = 7;
</lang>
 
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
Arrays can 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>
 
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.
 
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> program muTest
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>
 
Example run:
$ <span style="color: blue;">g95 -o mu matrixUtil.f95 muTest.f95</span>
$ <span style="color: blue;">./mu</span>
<span style="color: red;">15</span>
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. 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. 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. 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. 0. 0.
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.
0. 0. 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. 0. 0. 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. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.
$
 
 
=={{header|Haskell}}==