History

moving examples to top dir

Ge Wang committed 9 years ago

Commit 35ca2f61 parent fd011ef486 files · +-44 −-44Browse files

Showing the changes to examples/array (8 of 486 files). Show all

Unified · Split

8 files
  1. examples/array/array_argument.ck +19 −0
  2. examples/array/array_assign.ck +13 −0
  3. examples/array/array_dynamic.ck +20 −0
  4. examples/array/array_mdim.ck +14 −0
  5. examples/array/array_mmixed.ck +17 −0
  6. examples/array/array_resize.ck +35 −0
  7. examples/array/array_storage.ck +9 −0
  8. examples/array/array_sub_assign.ck +14 −0
examples/array/array_argument.ck+19−0
@@ -0,0 +1,19 @@
1+// array pass by reference test - should change values?
2+
3+// function that takes array
4+fun void mirror( int arg[] )
5+{
6+ // assign 0th element to 1st element
7+ arg[0] => arg[1];
8+}
9+
10+// declare array
11+int foo[2];
12+2 => foo[0];
13+1 => foo[1];
14+
15+// call the function
16+mirror( foo );
17+
18+// test
19+if ( foo[0] == foo[1] ) <<<"success">>>;
examples/array/array_assign.ck+13−0
@@ -0,0 +1,13 @@
1+// 17.ck : partial array
2+
3+// declare int array 2x2
4+int x[2][2];
5+
6+// assign
7+2 => x[1][1];
8+
9+// assign partial array reference to 'y'
10+x[1] @=> int y[];
11+
12+// test
13+if( y[1] == 2 ) <<<"success">>>;
examples/array/array_dynamic.ck+20−0
@@ -0,0 +1,20 @@
1+// instantiate int array
2+float argh[0];
3+
4+// print out relevant info
5+<<< "array size:", argh.size() >>>;
6+
7+// append items (array should grow dynamically as needed)
8+argh << 3.0 << 4 << 5;
9+
10+// print out relevant info
11+<<< "array size:", argh.size() >>>;
12+
13+// print out elements
14+<<< "contents:", argh[0], argh[1], argh[2] >>>;
15+
16+// pop element
17+argh.popBack();
18+
19+// print out relevant info
20+<<< "array size:", argh.size() >>>;
examples/array/array_mdim.ck+14−0
@@ -0,0 +1,14 @@
1+// 15.ck : multi-dimensional array instantiation
2+
3+// delcare int array 2x2x2
4+int a[2][2][2];
5+// delcare float array 2x2x2
6+float b[2][2][2];
7+// assign
8+5 => a[1][1][1] => b[1][1][1];
9+
10+// multi-dimensional array initialization
11+[ [1,2], [3,4] ] @=> int c[][];
12+
13+// test
14+if( a[1][1][1] == 5 && b[1][1][1] == 5.0 && c[0][1] == 2 ) <<<"success">>>;
examples/array/array_mmixed.ck+17−0
@@ -0,0 +1,17 @@
1+// based on Robin Haberkorn's example
2+int a[0][2];
3+
4+// instantiate arrays
5+new int[2] @=> a["foo"];
6+new int[2] @=> a["bar"];
7+
8+// fill them
9+23 => a["foo"][0];
10+24 => a["foo"][1];
11+32 => a["bar"][0];
12+42 => a["bar"][1];
13+
14+// test them
15+if( a["foo"][0] == 23 && a["bar"][1] == 42 )
16+ <<< "success" >>>;
17+
examples/array/array_resize.ck+35−0
@@ -0,0 +1,35 @@
1+// instantiate a float array
2+float argh[0];
3+
4+// print
5+<<< "array size:", argh.size() >>>;
6+
7+// resize
8+4 => argh.size;
9+
10+// print
11+<<< "array size:", argh.size() >>>;
12+
13+// set elements
14+3.0 => argh[0];
15+4 => argh[1];
16+5.0 => argh[2];
17+6 => argh[3];
18+
19+// print
20+<<< "contents:", "">>>;
21+for( int i; i < argh.size(); i++ )
22+ <<< "argh[", i, "]:", argh[i] >>>;
23+
24+// recap
25+2 => argh.size;
26+
27+// print
28+<<< "array size:", argh.size() >>>;
29+
30+// clear the contents
31+argh.clear();
32+
33+// print
34+<<< "array size:", argh.size() >>>;
35+
examples/array/array_storage.ck+9−0
@@ -0,0 +1,9 @@
1+// multi-dimensional array
2+int a[2][2][2];
3+// assignment
4+4 => a[0][0][1];
5+// access
6+a[0][0][1] => int b;
7+
8+// test
9+if ( b == 4 ) <<<"success">>>;
examples/array/array_sub_assign.ck+14−0
@@ -0,0 +1,14 @@
1+// multi-dimensional array
2+// array to array assignment
3+
4+int a[2][2][2];
5+int b[2][2];
6+// assignment
7+1 => b[0][1];
8+2 => b[0][0];
9+
10+// assign b to 0th element of a
11+b @=> a[0];
12+
13+// verify
14+if ( a[0][0][1] == 1 ) <<<"success">>>;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t