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

Last change on this file since 74cbaa3 was c3e41cda, checked in by Peter A. Buhr <pabuhr@…>, 2 weeks ago

proofread new material in background and array chapters

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include <fstream.hfa>
2#include <stdlib.hfa>
3#include <array.hfa>
4#include <string.hfa>
5
6
7
8
9
10forall( [C], [S] ) $\C{// num courses, num students}$
11struct School {
12        @array@( string, C ) course_codes; $\C{// nested VLAs}$
13        @array@( string, S ) student_ids;
14        @array@( int, C, S ) preferences; $\C{// multidimensional}$
15};
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30forall( [C], [S] )
31int getPref( @School( C, S ) & school@, int is, int pref ) {
32        for ( ic; C ) {
33                int curPref = @school.preferences@[ic][is];   $\C{// offset calculation implicit}$
34                if ( curPref == pref ) return ic;
35        }
36        assert( false );
37}
38
39
40
41
42
43
44
45
46
47
48
49
50int main() {
51        int nc, ns;
52        sin | nc | " courses,";
53        sin | ns | " students";
54        @School( nc, ns ) school;@
55        // ... elided: read data into school
56
57
58
59
60        {       string sv;
61                int iv;
62                // headers' row
63                sin | "\nc\\s";
64                for ( is ; ns ) {
65                        // column label
66                        sin | sv;
67                        school.student_ids[is] = sv;
68                }
69                // body rows
70                for ( ic ; nc ) {
71                        // row label
72                        sin | sv;
73                        school.course_codes[ic] = sv;
74                        for ( is ; ns ) {
75                                // matrix item
76                                sin | iv;
77                                school.preferences[ic][is] = iv;
78                        }
79                }
80        }
81
82
83
84
85
86
87
88
89
90        for ( is; ns ) {
91                sout | school.student_ids[is] | ": " | nonl;
92                for ( pref; 1 ~= nc ) {
93                        int ic = getPref(school, is, pref);
94                        sout | school.course_codes[ ic ] | nonl;
95                }
96                sout | nl;
97        }
98}
99
100
101// Local Variables: //
102// compile-command: "sed -f sedcmd hello-accordion.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" //
103// End: //
Note: See TracBrowser for help on using the repository browser.