1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <cstdio>
const int numYears = 4;
const int startingYear = 2011;
typedef struct {
const char* label;
float dataGDP[numYears];
} data;
data datas[] = {
{"manufacturing", {65.9,68.1,65.5,67.8}},
{"construction", {14.8,16.4,17.7,18.9}},
{"utilities", {5.16,5.3,5.11,5.18}},
{"wholesale", {66.5,64.6,66.2,64.4}},
{"transportation and storage", {22.2,23.6,24.3,25.3}},
};
const int numDatas = sizeof(datas)/sizeof(data);
float calcTotalGDPForYear(int year) {
float totalGDP = 0;
for(int dept = 0; dept < numDatas; ++dept) {
totalGDP += datas[dept].dataGDP[year];
}
return totalGDP;
}
void displayPercent(void)
{
for(int year = 0; year < numYears; ++year) {
printf("\n\nYear %d:\n", year + startingYear);
float totalGDP = calcTotalGDPForYear(year);
for(int dept = 0; dept < numDatas; ++dept) {
float percentage = (datas[dept].dataGDP[year]/totalGDP)*100;
printf("The percentage for %s is %.2f.\n", datas[dept].label, percentage);
}
}
}
int main() {
displayPercent();
}
| |