Computers in Engineering WWW Site - Example 14.4

# Example 14.4

#### C Version

```/*
Functions to find the average of parts of an array.
*/

#include <stdio.h>

typedef char char_array[12][4];  /* Variable Type Declaration */
typedef short int_array[12];
typedef double real_array[12];

char_array month;      /*  Declaration Statements  */
int_array units;
real_array sales;
short k, start, meanu, j;
double save;

void average(n, start, units, sales, iave, ave)
short n, start;
short *units;                  /*  Procedure Declaration  */
double *sales;
short *iave;
double *ave;
{
short cars, k, j;
double dollars;

cars = 0;
dollars = 0.0;
j = start + n - 1;
for (k = start - 1; k < j; k++)
{
cars += units[k];      /* To compute the sum */
dollars += sales[k];
}  /*  End of for{} loop  */

*iave = cars / n;     /*  To compute the average  */
*ave = dollars / n;
}   /*  End of average procedure  */

main()
{
/*  Declaration Statement  */
char *TEMP;

clrscr();
printf("C67.C -> Average Program 2 (again using a function)\n\n");

/*  Assignment Statements  */

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

for (k=1;k<=12; k++)
printf(" %s%7d%6.1f\n", month[k - 1], units[k - 1], sales[k - 1]);

/*
Data has been read, stored and printed.
Now use procedure once for each quarter.
*/

for (j = 1; j <= 4; j++)
{
start = (j - 1) * 3 + 1;
average(3,start,units,sales,&meanu,&save);  /* Call of procedure */
printf("\nmean%7d%6.1f  quarter #%d\n", meanu, save, j);
}  /*  End of for{} loop  */

return(0);
}
/* End of main Program C67 */
/*
```
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 :
```
C67.C -> Average Program 2 (again using a function)

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

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

mean      3  31.0  quarter #1

mean      5  25.3  quarter #2

mean     13  11.3  quarter #3

mean      2  15.3  quarter #4

*/
```