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

Last change on this file since e15293b was caa3e2c, checked in by Peter A. Buhr <pabuhr@…>, 4 months ago

proofread array chapter and update example programs

  • Property mode set to 100644
File size: 1.6 KB
Line 
1#include <fstream.hfa>
2#include <stdlib.hfa>
3#include <array.hfa>
4#include <locale.h>                                                     // setlocale
5
6
7
8
9
10forall( T, @[NprovTerty]@, @[Nmunicipalities]@ )
11struct CanPop {
12        array( T, @NprovTerty@ ) provTerty; $\C{// nested VLA}$
13        array( T, @Nmunicipalities@ ) municipalities; $\C{// nested VLA}$
14        int total_pt, total_mun;
15};
16
17
18// TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?)
19
20forall( T, [NprovTerty], [Nmunicipalities] )
21        void ?{}( T &, CanPop( T, NprovTerty, Nmunicipalities ) & this ) {}
22
23forall( T &, [NprovTerty], [Nmunicipalities] )
24        void ^?{}( CanPop( T, NprovTerty, Nmunicipalities ) & this ) {}
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40forall( T, [NprovTerty], [Nmunicipalities] )
41void check( CanPop( T, NprovTerty, Nmunicipalities ) & pop ) with( pop ) {
42        total_pt = total_mun = 0;
43        for ( i; NprovTerty ) total_pt += provTerty[i];
44        for ( i; Nmunicipalities ) total_mun += municipalities[i];
45}
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60int main( int argc, char * argv[] ) {
61        const int npt = ato( argv[1] ), nmun = ato( argv[2] );
62        @CanPop( int, npt, nmun ) pop;@
63        // read in population numbers
64        @check( pop );@
65        sout | setlocale( LC_NUMERIC, getenv( "LANG" ) );
66        sout | "Total province/territory:" | pop.total_pt;
67        sout | "Total municipalities:" | pop.total_mun;
68}
69/*
70$\$$ ./a.out  13  3573
71Total province/territory: 36,991,981
72Total municipalities: 36,991,981
73$\$$ ./a.out  13  3654
74Total province/territory: 36,991,981
75Total municipalities: 36,991,981
76*/
77
78// Local Variables: //
79// compile-command: "sed -f sedcmd hello-accordion.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" //
80// End: //
Note: See TracBrowser for help on using the repository browser.