Please help here.

Basically what the program required is to find the percentage of each column of the dataGDP with respect to each column's total GDP. Is there any way I can shorten this program? Using Loop or any other suggestion?

//external data
float dataGDP[5][4]={
{65.9,68.1,65.5,67.8},//data for m
{14.8,16.4,17.7,18.9},//data for C
{5.16,5.3,5.11,5.18},//data for U
{66.5,64.6,66.2,64.4},//data for W
{22.2,23.6,24.3,25.3}//data for T
};

float totalGDP[]={174.56,178,178.81,181.58}

void displayPercent(void)
{
int year=0;
float percentageM=0;
float percentageC=0;
float percentageU=0;
float percentageW=0;
float percentageT=0;

if (year==2011)
percentageM= (dataGDP[0][0]/totalGDP[0])*100;
percentageC=(dataGDP[1][0]/totalGDP[0])*100;
percentageU=(dataGDP[2][0]/totalGDP[0])*100;
percentageW=(dataGDP[3][0]/totalGDP[0])*100;
percentageT=(dataGDP[4][0]/totalGDP[0])*100;

if (year==2012)
percentageM= (dataGDP[0][1]/totalGDP[1])*100;
percentageC=(dataGDP[1][1]/totalGDP[1])*100;
percentageU=(dataGDP[2][1]/totalGDP[1])*100;
percentageW=(dataGDP[3][1]/totalGDP[1])*100;
percentageT=(dataGDP[4][1]/totalGDP[1])*100;

if(year==2013)
percentageM= (dataGDP[0][2]/totalGDP[2])*100;
percentageC=(dataGDP[1][2]/totalGDP[2])*100;
percentageU=(dataGDP[2][2]/totalGDP[2])*100;
percentageW=(dataGDP[3][2]/totalGDP[2])*100;
percentageT=(dataGDP[4][2]/totalGDP[2])*100;

if(year==2014)
percentageM= (dataGDP[0][3]/totalGDP[3])*100;
percentageC=(dataGDP[1][3]/totalGDP[3])*100;
percentageU=(dataGDP[2][3]/totalGDP[3])*100;
percentageW=(dataGDP[3][3]/totalGDP[3])*100;
percentageT=(dataGDP[4][3]/totalGDP[3])*100;

printf("The percentage for manufactoring is %.2f.\n The percentage for construction is %.2f.\n The percentage for utilities is %.2f.\n The percentage for wholesale & retail trade is %.2f.\n The percentage for Transportation &storage is %.2f.\n" ,percentageM,percentageC,percentageU,percentageW,percentageT);

}//
Last edited on
Hello, and welcome to cplusplus.com. Please use the <> to the right of the text box when posting code.

And yes it can be shortened with loops.

In a moment, I will add how to do it as an edit.
Edit:
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();
}
Last edited on
Perhaps something like following?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void displayPercent(void) {
	int year = 2012;
	float p[5];
	char* m[5] = {
		"manufactoring", 
		"construction",
		"utilities",
		"wholesale & retail trade",
		"Transportation & storage"
	};
	
        // it's critical that year is between 2011 and 2014
	if (!(year >= 2011 && year <= 2014))
		return;
       
        // calculate GDP for the current year
	int k = year - 2011;
	for (int j=0; j<5; ++j)
		p[j] = 100 * dataGDP[j][k] / totalGDP[k];

        // print it out
	for (int j=0; j<5; ++j)
		printf("The precntage for %s is %.2f\n", m[j], p[j]);
}
Last edited on
thank you guys for the help
Topic archived. No new replies allowed.