Computers in Engineering WWW Site - Example 13.3

# Example 13.3

#### C Version

```/*
How to sum part of an array.
*/

#include <stdio.h>

main()
{
/*  Declaration Statements  */
char month[12][4];
short units[12];
short uq[4];      /*  uq stands for units per quarter  */
double sales[12];
double sq[4];     /*  dq stands for sales per quarter  */
short i, j, k;
char *TEMP;

printf("C53.C -> Summing part of an array \n");

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

/*  Form 4 quarter totals  */

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  */

/*  Print results  */
printf("\n Cars sold by quarter %5d%5d%5d%5d\n", uq[0], uq[1], uq[2],
uq[3]);
printf("\n Sales by quarter     % .1E% .1E% .1E% .1E\n", sq[0], sq[1],
sq[2], sq[3]);

return(0);
}
/* End of Program C53 */
/*
```
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 :
```
C53.C -> Summing part of 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

Cars sold by quarter    10   16   39    6

Sales by quarter      9E+01 8E+01 3E+01 5E+01

*/
```