Last change
on this file since 4b75ae9 was
caa3e2c,
checked in by Peter A. Buhr <pabuhr@…>, 4 weeks ago
|
proofread array chapter and update example programs
|
-
Property mode set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[dab9fb93] | 1 | #include <fstream.hfa> |
---|
| 2 | #include <array.hfa> |
---|
[bbf6a180] | 3 | |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | |
---|
[a5e2682] | 34 | |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | forall( [N] ) |
---|
[e26a842] | 41 | void print1d_cstyle( array( float, N ) & r ); $\C{// C style}$ |
---|
[bbf6a180] | 42 | |
---|
[e26a842] | 43 | forall( [N], C & @| ar( C, float, N )@ ) $\C{// add trait}$ |
---|
[bbf6a180] | 44 | void print1d( C & c ); |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | |
---|
[a5e2682] | 60 | forall( [N] ) |
---|
[e26a842] | 61 | void print1d_cstyle( array( float, N ) & r ) { $\C{// C style}$ |
---|
| 62 | for ( i; N ) sout | r[i] | nonl; |
---|
[5546f50b] | 63 | sout | nl; |
---|
[bbf6a180] | 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | |
---|
[e26a842] | 77 | |
---|
| 78 | |
---|
[a5e2682] | 79 | forall( [N], C & | ar( C, float, N ) ) |
---|
[bbf6a180] | 80 | void print1d( C & c ) { |
---|
[5546f50b] | 81 | for( i; N ) { |
---|
| 82 | sout | c[i] | nonl; |
---|
| 83 | } |
---|
| 84 | sout | nl; |
---|
[bbf6a180] | 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | |
---|
[e26a842] | 100 | void fill( array( float, 5, 7 ) & a ) { |
---|
[5546f50b] | 101 | for ( i; (ptrdiff_t) 5 ) { |
---|
| 102 | for ( j; 7 ) { |
---|
| 103 | a[i,j] = 1.0 * i + 0.1 * j; |
---|
| 104 | sout | a[[i,j]] | nonl; |
---|
| 105 | } |
---|
| 106 | sout | nl; |
---|
| 107 | } |
---|
| 108 | sout | nl; |
---|
[bbf6a180] | 109 | } |
---|
| 110 | |
---|
| 111 | int main() { |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
[e26a842] | 118 | array( float, 5, 7 ) m; |
---|
| 119 | fill( m ); |
---|
[bbf6a180] | 120 | /* |
---|
[e26a842] | 121 | r/c 0 1 2 3 4 5 6 |
---|
| 122 | 0 0.0 0.1 0.2 @0.3@ 0.4 0.5 0.6 |
---|
| 123 | 1 1.0 1.1 1.2 @1.3@ 1.4 1.5 1.6 |
---|
| 124 | 2 @2.0 2.1 2.2 2.3 2.4 2.5 2.6@ |
---|
| 125 | 3 3.0 3.1 3.2 @3.3@ 3.4 3.5 3.6 |
---|
| 126 | 4 4.0 4.1 4.2 @4.3@ 4.4 4.5 4.6 |
---|
[bbf6a180] | 127 | */ |
---|
[5546f50b] | 128 | |
---|
[bbf6a180] | 129 | |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | |
---|
[dab9fb93] | 139 | |
---|
[caa3e2c] | 140 | print1d_cstyle( @m[ 2 ]@ ); $\C{// row 2: 2.0 2.1 2.2 2.3 2.4 2.5 2.6}$ |
---|
[bbf6a180] | 141 | |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | |
---|
[e26a842] | 145 | print1d( m[ 2 ] ); $\C{// row: 2.0 2.1 2.2 2.3 2.4 2.5 2.6}$ |
---|
[bbf6a180] | 146 | |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | |
---|
[e26a842] | 150 | print1d( m[ 2, all ] ); $\C{// row 2: 2.0 2.1 2.2 2.3 2.4 2.5 2.6}$ |
---|
| 151 | print1d( m[ all, 3 ] ); $\C{// column 3: 0.3 1.3 2.3 3.3 4.3}$ |
---|
[bbf6a180] | 152 | |
---|
| 153 | |
---|
| 154 | |
---|
[e26a842] | 155 | print1d_cstyle( m[ 2, all ] ); |
---|
[bbf6a180] | 156 | |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | |
---|
[a5e2682] | 163 | #ifdef SHOW_ERROR_1 |
---|
[bbf6a180] | 164 | |
---|
[e26a842] | 165 | print1d_cstyle( m[ all, 2 ] ); $\C{// bad}$ |
---|
[bbf6a180] | 166 | |
---|
| 167 | #endif |
---|
| 168 | |
---|
| 169 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.