Arrays of structures: Illustration of subscripted structure variables

Program 49) For the student array. Writ a program t calculate the subject-wise and student-wise totals and store them as a part of the structure.
 This program is shown below. We have declared a four-member structure, the fourth one for keeping the student-totals. We have also declared as array total o keep the subject totals and the grand total. The grand total is given by total.total. Note that a member name can be any valid C name and can be the same as an existing structure variables names. The linked name total.total represents the total member of the structure variable total.
                                                                                     
              student [0].subject 1         |           45         |
                                 .subject 2         |         68          |
                                 .subject 3         |          81          |
              student [1].subject 1         |          75          |
                                 .subject 2         |          53          |
                                 .subject 3         |          69          |
              student [2].subject 1         |          57          |
                                 .subject2          |          36          |
                                 .subject 3         |          71          |   

                       The array student inside memory 

                                      PROGRAM

struct marks
{
        int sub1 ;
        int sub2 ;
        int sub3 ;
        int total ;
} ;
main ( )
 {
       int i ;
       struct marks student [3] = { {45,67,81,0},
                                                       {75,53,69,0},
                                                       {57,36,71,0} } ;
      struct marks total ;
      for (i = 0; i <= 2; i++)
      {
             student [i].total = student [i].sub1 +
                                            student [i].sub2 +
                                            student [i]sub3 ;
             total.sub1 = total . sub1 + student [i] . sub1 ;
             total.sub2 = total . sub2 + student [i] . sub2 ;
             total.sub3 = total . sub3 + student [i] . sub3 ;
             total.total = total . total + student [i] . total ;
     }
     printf ("  STUDENT             TOTAL\n\n") ;
     for (i = 0; i <= 2; i++)
            printf ("Student [%d]      %d\n", i + 1, 
                                    student [i] . total) ;
     printf ("\n  SUBJECT            TOTAL\n\n") ;
     printf ("%s    %d\n%s       %d\n%s        %d\n",
                                 "Subject 1   ", total.sub1,
                                 "Subject 2   ", total.sub2,
                                 "Subject 3   ", total.sub3) ;
   printf ("\nGrand Total = %d\n", total.total) ;
 }

Output:            STUDENT             TOTAL
                         Student [1]              193
                         Student [2]              197
   
                         Student [3]               164

                         SUBJECT               TOTAL 
                         Subject 1                   177
                         Subject 2                   156
                         Subject 3                   221

                         Grand total      =    554         

No comments:

Post a Comment