Computers in Engineering WWW Site - Example 13.4

Example 13.4


C Version

/*
  Several operations on an array (largest/smallest, average, etc)
*/

#include <stdio.h>

main()
{
  /*  Declaration Statements  */
  char month[12][4];
  short units[12];
  double sales[12];
  short uq[4];
  double sq[4];
  short i, cars, mins, maxs, j, k, iave;
  double totals, big, min, ave;

  printf("C54.C -> Operations an array \n");

  /*  Assignment Statements  */

  for (i = 1; i <= 12; i++)
  {                                      /* Read value from */
    printf("Month (Jan,Feb,...) : ");    /* keyboard entry. */
    scanf("%s",month[i-1]);
    printf("Units Sold          : ");
    scanf("%hd", &units[i - 1]);
    printf("Sales (in million $): ");
    scanf("%lg", &sales[i - 1]);
  }

  /*  Initializing variables  */
  cars = units[0];
  totals = sales[0];
  big = totals;
  min = totals;
  mins = 1;
  maxs = 1;

  for (i = 2; i <= 12; i++)
  {
    cars += units[i - 1];
    totals += sales[i - 1];
    if (sales[i - 1] > big)     /* It selects the biggest sales  */
    {                           /* and keep track of the index   */
      big = sales[i - 1];       /* to know when it occurs.       */
      maxs = i;
    }  /*  End of if statement  */
    else
    {
       if (sales[i - 1] < min)  /* It selects the smallest sales */
       {                        /* and keep track of the index   */
       min = sales[i - 1];      /* to know when it occurs.       */
       mins = i;
       }
    }  /*  End of else statement  */
  }  /*  End of for{} loop  */

  i = 0;
  for (j = 1; j <= 4; j++)
  {
    uq[j - 1] = 0;
    sq[j - 1] = 0.0;
    for (k = 1; k <= 3; k++)
    {
      i++;
      uq[j - 1] += units[i - 1];
      sq[j - 1] += sales[i - 1];
    }  /*  End of inner for{} loop  */
  }  /*  End of outer for{} loop  */

  /*  Output original data and results  */

  printf("\n");
  printf("Month unit  sales m$\n");
  for (k = 1; k <= 12; k++)
    printf("%3c%s%6d%6.1f\n",' ', month[k-1], units[k-1], sales[k-1]);
  printf("\nTotals%5d%6.1f\n", cars, totals);

  iave = cars / 12;      /*  Compute the units sold average */
  ave = totals / 12.0;   /*  and sales average by month.    */

  printf("\Nave.%7d%6.1f\n\n", iave, ave);
  printf("Best/worst-%5.1f%5.1f\n\n", big, min);
  printf("Occurred in%2c%s%2c%s\n\n",
          ' ', month[maxs - 1], ' ', month[mins - 1]);
  printf("Cars sold by quarter%5d%5d%5d%5d\n", uq[0], uq[1], uq[2],
          uq[3]);
  printf("Sales by quarter    %5.1f%5.1f%5.1f%5.1f\n",
          sq[0], sq[1], sq[2], sq[3]);

  return(0);
}
/*  End of Program C54  */
/*
INPUT :

Jan
5
12
Feb
4
78
Mar
1
3
Apr
5
32
May
6
12
June
5
32
July
1
12
Aug
34
12
Sep
4
10
Oct
5
23
Nov
1
23
Dec
0
0

OUTPUT :

C54.C -> Operations on an array

Month (Jan,Feb,...) : Jan
Units Sold          : 5
Sales (in million $): 12
Month (Jan,Feb,...) : Feb
Units Sold          : 4
Sales (in million $): 78
Month (Jan,Feb,...) : Mar
Units Sold          : 1
Sales (in million $): 3
Month (Jan,Feb,...) : Apr
Units Sold          : 5
Sales (in million $): 32
Month (Jan,Feb,...) : May
Units Sold          : 6
Sales (in million $): 12
Month (Jan,Feb,...) : June
Units Sold          : 5
Sales (in million $): 32
Month (Jan,Feb,...) : July
Units Sold          : 1
Sales (in million $): 12
Month (Jan,Feb,...) : Aug
Units Sold          : 34
Sales (in million $): 12
Month (Jan,Feb,...) : Sep
Units Sold          : 4
Sales (in million $): 10
Month (Jan,Feb,...) : Oct
Units Sold          : 5
Sales (in million $): 23
Month (Jan,Feb,...) : Nov
Units Sold          : 1
Sales (in million $): 23
Month (Jan,Feb,...) : Dec
Units Sold          : 0
Sales (in million $): 0

Month unit  sales m$
   Jan     5  12.0
   Feb     4  78.0
   Mar     1   3.0
   Apr     5  32.0
   May     6  12.0
   June    5  32.0
   July    1  12.0
   Aug    34  12.0
   Sep     4  10.0
   Oct     5  23.0
   Nov     1  23.0
   Dec     0   0.0

Totals    71 249.0
Nave.      5  20.8

Best/worst- 78.0  0.0

Occurred in  Feb  Dec

Cars sold by quarter   10   16   39    6
Sales by quarter     93.0 76.0 34.0 46.0

*/

Last modified: 22/07/97