source: doc/theses/mike_brooks_MMath/programs/hello-accordion.cfa @ dab9fb93

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

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

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[dab9fb93]1#include <fstream.hfa>
2#include <stdlib.hfa>
3#include <array.hfa>
[bbf6a180]4
5
6
7
8
9
10
[dab9fb93]11forall( T, [Nclients], [Ncosts] )
[bbf6a180]12struct request {
13    unsigned int requestor_id;
[dab9fb93]14    array( T, Nclients ) impacted_client_ids; // nested VLA
15    array( float, Ncosts ) cost_contribs; // nested VLA
[bbf6a180]16    float total_cost;
17};
18
19
20// TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?)
21
[dab9fb93]22forall( T, [Nclients], [Ncosts] )
23        void ?{}( T &, request( T, Nclients, Ncosts ) & this ) {}
[bbf6a180]24
[dab9fb93]25forall( T &, [Nclients], [Ncosts] )
26        void ^?{}( request( T, Nclients, Ncosts ) & this ) {}
[bbf6a180]27
28
29
30
31
32
33
34
35
36
37
38
39
40
[dab9fb93]41forall( T, [Nclients], [Ncosts] )
42void summarize( request( T, Nclients, Ncosts ) & r ) {
[bbf6a180]43    r.total_cost = 0;
[dab9fb93]44    for( i; Ncosts )
[bbf6a180]45        r.total_cost += r.cost_contribs[i];
46    // say the cost is per-client, to make output vary
[dab9fb93]47    r.total_cost *= Nclients;
[bbf6a180]48}
49
50
51
52
53
54
55
56
57
58
59
60
[dab9fb93]61int main( int argc, char * argv[] ) {
62        const int ncl = ato( argv[1] );
63        const int nco = 2;
[bbf6a180]64
[dab9fb93]65        request( int, ncl, nco ) r;
66        r.cost_contribs[0] = 100;
67        r.cost_contribs[1] = 0.1;
[bbf6a180]68
[dab9fb93]69        summarize(r);
70        sout | "Total cost:" | r.total_cost;
71}
[bbf6a180]72/*
[dab9fb93]73$\$$ ./a.out 5
[bbf6a180]74Total cost: 500.5
[dab9fb93]75$\$$ ./a.out 6
[bbf6a180]76Total cost: 600.6
77*/
Note: See TracBrowser for help on using the repository browser.