Skip to content

Commit 2c8a570

Browse files
authored
Improve array slicing and examples example (#502)
1 parent 5001025 commit 2c8a570

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

source/learn/quickstart/arrays_strings.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,37 @@ program array_slice
4949
5050
integer :: i
5151
integer :: array1(10) ! 1D integer array of 10 elements
52+
5253
integer :: array2(10, 10) ! 2D integer array of 100 elements
5354
5455
array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ! Array constructor
55-
array1 = [(i, i = 1, 10)] ! Implied do loop constructor
56+
print *, "Array at odd indices"
57+
print *, array1(1:10:2) ! Print out elements at odd indices, start at element 1, go up to element 10, in strides of 2
58+
59+
print *, "Array in reverse!"
60+
print *, array1(10:1:-1) ! Print an array in reverse
61+
5662
array1(:) = 0 ! Set all elements to zero
63+
print *, "Reset array to 0!"
64+
print *, array1
65+
66+
array1 = [(i, i = 1, 10)] ! Implied do loop constructor
67+
print *, "Now back to 1 to 10!"
68+
print *, array1
69+
70+
5771
array1(1:5) = 1 ! Set first five elements to one
58-
array1(6:) = 1 ! Set all elements after five to one
72+
print *, "Set first five elements to 1"
73+
print *, array1
74+
75+
array1(6:) = 2 ! Set all elements after five to two
76+
print *, "Set all elements after 5 to 2"
77+
print *, array1
5978
60-
print *, array1(1:10:2) ! Print out elements at odd indices
79+
80+
print *, "First column of 2d array"
6181
print *, array2(:,1) ! Print out the first column in a 2D array
62-
print *, array1(10:1:-1) ! Print an array in reverse
82+
6383
6484
end program array_slice
6585
```

0 commit comments

Comments
 (0)