Showing the changes to examples/array (1 of 6 files). Show all
Unified · Split
| @@ -0,0 +1,26 @@ | |||
| 1 | // example reversing the order of an array | ||
| 2 | // version: need chuck-1.4.2.1 or higher | ||
| 3 | |||
| 4 | // an array | ||
| 5 | [1,2,3,4,5] @=> int a[]; | ||
| 6 | |||
| 7 | // print array in-order | ||
| 8 | print_arr(a); | ||
| 9 | |||
| 10 | // reverse | ||
| 11 | a.reverse(); | ||
| 12 | |||
| 13 | // print reversed order | ||
| 14 | print_arr(a); | ||
| 15 | |||
| 16 | // return to original order | ||
| 17 | a.reverse(); | ||
| 18 | print_arr(a); | ||
| 19 | |||
| 20 | |||
| 21 | fun void print_arr(int a[]) { | ||
| 22 | for (0 => int i; i < a.size(); i++) { | ||
| 23 | cherr <= a[i] <= " "; | ||
| 24 | } | ||
| 25 | cherr <= IO.nl(); | ||
| 26 | } | ||