source: doc/theses/mike_brooks_MMath/programs/hello-array.cfa @ 40ab446

Last change on this file since 40ab446 was dab9fb93, checked in by Michael Brooks <mlbrooks@…>, 10 months ago

Accept Peter's proofreading and adjustment of examples to current syntax

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[dab9fb93]1#include <fstream.hfa>
2#include <stdlib.hfa>
3#include <array.hfa> // learned has to come afer stdlib, which uses the word tag
[bbf6a180]4
5// Usage:
6// ./a.out 5
7// example does an unchecked reference to argv[1]
8
9
[dab9fb93]10forall( [N] ) // array bound
[bbf6a180]11array(bool, N) & f( array(float, N) & a, array(float, N) & b ) {
[dab9fb93]12    array(bool, N) & ret = *alloc(); // sizeof used by alloc
13    for( i; N ) {
14        ret[i] = 0.005 > 2 * (abs(a[i] - b[i])) / (abs(a[i]) + abs(b[i]));
[bbf6a180]15    }
16    return ret;
17}
18
19
20
21
22
23
24
25
26
27
28// TODO: standardize argv
29
[dab9fb93]30int main( int argc, char * argv[] ) {
31    int n = ato( argv[1] );
32    array(float, n) a, b; // VLA
33    for ( i; n ) {
34        a[i] = 3.14 / (i + 1);
[bbf6a180]35        b[i] = a[i] + 0.005 ;
36    }
[dab9fb93]37    array(bool, n) & result = f( a, b ); // call
38    sout | "result: " | nonl;
39    for ( i; n )
40        sout | result[i] | nonl;
41    sout | nl;
42    free( &result ); // free returned storage
[bbf6a180]43}
44/*
[dab9fb93]45$\$$ ./a.out 5
46result: true true true false false
47$\$$ ./a.out 7
48result: true true true false false false false
[bbf6a180]49*/
50
[dab9fb93]51void fred() {
52        array(float, 10) a;
53        array(float, 20) b;
[bbf6a180]54    f( a, a );
55    f( b, b );
[dab9fb93]56    f( a, b );
[bbf6a180]57}
58
59#ifdef SHOWERR1
[dab9fb93]60forall( [M], [N] )
[bbf6a180]61void bad( array(float, M) &a, array(float, N) &b ) {
62    f( a, a ); // ok
63    f( b, b ); // ok
64    f( a, b ); // error
65}
66#endif
67
68
69
[dab9fb93]70forall( [M], [N] )
71void bad_fixed( array(float, M) & a, array(float, N) & b ) {
72    if ( M == N ) {
73        f( a, (array(float, M) &)b ); // cast b to matching type
[bbf6a180]74    }
[dab9fb93]75}
Note: See TracBrowser for help on using the repository browser.