Computers in Engineering WWW Site - Example 14.7

Example 14.7


C Version

/*
  Calculations on multidimensional arrays.
*/

#include <stdio.h>

/* Array initialization : */

char *season[]=
{
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
};

short units[12]=
{
{672},{609},{715},{803},{810},{831},
{829},{727},{780},{703},{791},{783}
};

double revenu[12]=
{
{3.4},{3.2},{3.7},{4.2},{4.8},{5.1},
{5.1},{5.1},{4.3},{3.9},{4.2},{3.6}
};

main()
{
  /*  Define auxiliary arrays :  */

  short icol[3], imean[3];
  double col[3], mean[3];

  /*  Declaration statements  */
  short row, column, igrand, itotal, i,j, iave, igave;
  double grand, total, ave, gave;
  char *TEMP;

  printf("C72.C -> Calculations on multidimensional arrays\n\n");

  printf(" Seasonal Summary\n");
  /*
       Do 4 quarter average + totals
  */
  igrand = 0;
  grand = 0.0;
  for (i = 1; i <= 4; i=i++)
  {
    itotal = 0;
    total = 0.0;
    for(j=3*i-2 ; j <= 3*i; j++)
    {
      itotal += units[j - 1];
      total += revenu[j - 1];
    }  /*  End of inner for{} loop  */

    igrand += itotal;
    grand += total;
    iave = itotal / 3;
    ave = total / 3.0;
     for(j=3*i-2 ; j <= 3*i; j++)
      printf("%5c%s", ' ', season[j - 1]);
    printf("%3ctotal%5cave\n", ' ', ' ');
     for(j=3*i-2 ; j <= 3*i; j++)
      printf("%8d", units[j - 1]);
    printf("%8d%8d\n", itotal, iave);
    printf("%2c", ' ');
     for(j=3*i-2 ; j <= 3*i; j++)
      printf("%6.1fM$", revenu[j - 1]);
    printf("%6.1fM$%6.1fM$\n\n", total, ave);
  } /*  End of outer for{} loop  */

  /*  Do same calculations for each column  */

  for (i = 1; i <= 3; i++)
  {
   icol[i - 1] = 0;
   col[i - 1] = 0.0;
   for(j=i ; j <= 12; j=j+3)
   {
   icol[i - 1] += units[j - 1];
   col[i - 1] += revenu[j - 1];
   }  /*  End of inner for{} loop  */
  imean[i - 1] = icol[i - 1] / 4;
  mean[i - 1] = col[i - 1] / 4.0;
  } /*   End of outer for{} loop   */

  igave = igrand / 12;
  gave = grand / 12.0;

  /*  Output column statistics  */

  putchar('\n');

  for (j = 1; j <= 50; j++) putchar('-');

  printf("\n%8d%8d%8d%8d%8d\n", icol[0], icol[1], icol[2], igrand,
          igave);
  printf("%2c%6.1fM$%6.1fM$%6.1fM$%6.1fM$%6.1fM$\n\n",
          ' ', col[0], col[1], col[2], grand, gave);
  printf("%8d%8d%8d%4ccolumn average\n", imean[0], imean[1], imean[2],
          ' ');
  printf("%2c%6.1fM$%6.1fM$%6.1fM$\n\n", ' ', mean[0], mean[1],
          mean[2]);

  for (j = 1; j <= 50; j++) putchar('=');
  printf("\n");
  return(0);
}
/*  End of Program C72  */
/*
OUTPUT :

C72.C -> Calculations on multidimensional arrays

 Seasonal Summary
     Jan     Feb     Mar   total     ave
     672     609     715    1996     665
     3.4M$   3.2M$   3.7M$  10.3M$   3.4M$

     Apr     May     Jun   total     ave
     803     810     831    2444     814
     4.2M$   4.8M$   5.1M$  14.1M$   4.7M$

     Jul     Aug     Sep   total     ave
     829     727     780    2336     778
     5.1M$   5.1M$   4.3M$  14.5M$   4.8M$

     Oct     Nov     Dec   total     ave
     703     791     783    2277     759
     3.9M$   4.2M$   3.6M$  11.7M$   3.9M$


--------------------------------------------------
    3007    2937    3109    9053     754
    16.6M$  17.3M$  16.7M$  50.6M$   4.2M$

     751     734     777    column average
     4.1M$   4.3M$   4.2M$

==================================================

*/

Last modified: 22/07/97