Ignore:
Timestamp:
Aug 16, 2024, 12:03:54 PM (10 days ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
cef5bfc
Parents:
661e7b0
Message:

update accordion program, small changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/mike_brooks_MMath/programs/hello-accordion.cfa

    r661e7b0 r4558df2  
    88
    99
    10 forall( T, @[NprovTerty]@, @[Nmunicipalities]@ )
    11 struct CanPop {
    12         array( T, @NprovTerty@ ) provTerty; $\C{// nested VLA}$
    13         array( T, @Nmunicipalities@ ) municipalities; $\C{// nested VLA}$
    14         int total_pt, total_mun;
     10forall( [C], [S] ) $\C{// Class size, Students in class}$
     11struct School {
     12        @array( int, C )@ classIds; $\C{// nested VLAs}$
     13        @array( int, S )@ studentIds;
     14        @array( int, C, S )@ preferences; $\C{// multidimensional}$
    1515};
     16
    1617
    1718
    1819// TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?)
    1920
    20 forall( T, [NprovTerty], [Nmunicipalities] )
    21         void ?{}( T &, CanPop( T, NprovTerty, Nmunicipalities ) & this ) {}
     21forall( [C], [S] )
     22void ?{}( School( C, S ) & this ) {}
    2223
    23 forall( T &, [NprovTerty], [Nmunicipalities] )
    24         void ^?{}( CanPop( T, NprovTerty, Nmunicipalities ) & this ) {}
     24forall( [C], [S] )
     25        void ^?{}( School( C, S ) & this ) {}
    2526
    2627
     
    3738
    3839
    39 
    40 forall( T, [NprovTerty], [Nmunicipalities] )
    41 void 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];
     40forall( [C], [S] )
     41void init( @School( C, S ) & classes@, int class, int student, int pref ) with( classes ) {
     42        classIds[class] = class; $\C{// handle dynamic offsets of fields within structure}$
     43        studentIds[student] = student;
     44        preferences[class][student] = pref;
    4545}
    4646
     
    5858
    5959
    60 int 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;
     60int main() {
     61        int classes, students;
     62        sin | classes | students;
     63        @School( classes, students ) school;@
     64        int class, student, preference;
     65        // read data into school calling init
     66        // for each student's class/preferences
     67        try {
     68                for ( ) {
     69                        sin | class | student | preference;
     70                        init( school, class, student, preference );
     71                }
     72        } catch( end_of_file * ) {}
     73        for ( s; students ) {
     74                sout | "student" | s | nonl;
     75                for ( c; classes ) {
     76                        sout | school.preferences[c][s] | nonl;
     77                }
     78                sout | nl;
     79        }
    6880}
     81
     82
     83
    6984/*
    70 $\$$ ./a.out  13  3573
    71 Total province/territory: 36,991,981
    72 Total municipalities: 36,991,981
    73 $\$$ ./a.out  13  3654
    74 Total province/territory: 36,991,981
    75 Total municipalities: 36,991,981
     85$\$$ cat school1
     862 2
     870 0 1
     881 0 7
     890 1 12
     901 1 13
     91$\$$ a.out < school1
     92student 0 1 7
     93student 1 12 13
     94
     95$\$$ cat school2
     963 3
     970 0 1
     981 0 7
     992 0 8
     1000 1 12
     1011 1 13
     1022 1 14
     1030 2 26
     1041 2 27
     1052 2 28
     106$\$$ a.out < school2
     107student 0 1 7 8
     108student 1 12 13 14
     109student 2 26 27 28
    76110*/
    77111
Note: See TracChangeset for help on using the changeset viewer.