loop help

I don't know why the loop is not working.It only shows me the percentage T.
so for this program user are suppose to select the year they wish to review.
after that it will show the percentage for each sector for that year.

and i am suppose to show: manufacturing=..% construction=..% utilities=..% wholesale and retail trade=..% transport and storage=..%
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
extern float dataGDP[5][4];
extern float totalGDP[];
extern int year;
extern char sector;
extern int total;
float percentageM=0;
float percentageC=0;
float percentageU=0;
float percentageW=0;
float percentageT=0;
void displayPercent(void)
{//for void 
    
    
      if(year==2011)
     {//for if loop
      const int year=0;
      const int total=0; 

      for(sector=0;sector<5;sector++)
       {//for loop

                  percentageM=(dataGDP[sector][year]/totalGDP[total])*100;
		  percentageC=(dataGDP[sector][year]/totalGDP[total])*100;
		  percentageU=(dataGDP[sector][year]/totalGDP[total])*100;
		  percentageW=(dataGDP[sector][year]/totalGDP[total])*100;
		  percentageT=(dataGDP[sector][year]/totalGDP[total])*100;

	   }//for loop 
	}//for if loop

      if (year==2012)
     {//for if loop
      const int year=1;
      const int total=1;
      for(sector=0;sector<5;sector++)
     { //for loop

           percentageM=(dataGDP[sector][year]/totalGDP[total])*100;
	   percentageC=(dataGDP[sector][year]/totalGDP[total])*100;
	   percentageU=(dataGDP[sector][year]/totalGDP[total])*100;
	   percentageW=(dataGDP[sector][year]/totalGDP[total])*100;
	   percentageT=(dataGDP[sector][year]/totalGDP[total])*100;
 
     }//for loop
    }//if loop

    

    printf("The percentage are %.2f   %.2f   %.2f   %.2f   %.2f.\n",percentageM,percentageC,percentageU,percentageW,percentageT);

	if (sector == 0)
	{	
		printf("and sector is Manufacturing\n");
	}
	if(sector==1)
	{
		printf("and sector is construction\m");
	}
	if(sector==2)
	{
		printf("and sector is utilities.\n");
	}
	if(sector==3)
	{
		printf("and sector is wholesales and retail trade\n");
	}

	if(sector==4)
	{
		printf("and sector is transportation and storage\n");
	}

}//for void 
	
Last edited on
The loop is working. You just do not show anything within the loop. Line 51 is outside the loop.

Hint: you do not need two loops. It'd suffice that the local year/total are adjusted. I strongly suggest that you name the local variables different from the globals.
Last edited on
Lines 2-13: You should avoid the use of globals.

Line 5: why is sector a char? Since it is an index, an int makes more sense.

Line 21,30: You're changing a variable in another module of the program. That a poor practice. Was sector by chance a value input by the user? If so, this is exactly why you should avoid globals.

Lines 24-28,40-44: Your percentages are single variables. After looping through the sectors, the percentages contain only the value from the last sector.

Lines 53,57,61,65,70: The value of sector is not valid here. The for loop changed the value of sector. At the bottom of the for loop (line 46) sector was incremented to 5.







Thank you everyone for the help :)
I'd suggest something like this:
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
void displayPercent(int year)
{           
    int y = 0;
    
    switch (year)
    {
        case 2011: y = 0; break;
        case 2012: y = 1; break;
        case 2013: y = 2; break;
        case 2014: y = 3; break;
    } 

    const char* sectorname[5] = 
    {
        "Manufacturing", "construction", "utilities", 
        "wholesales and retail trade", "transportation and storage"
    };
           
    printf("The percentages for %d are \n", year);

    for (int sector = 0; sector < 5; sector++)
    {
        float  percentage = (dataGDP[sector][y] / totalGDP[y]) * 100;
        printf("%30s = %.2f\n", sectorname[sector], percentage);
    }
    printf("\n");    
}
Hi Chervil,
Could I use the above short program that you posted For my assignment ?
You're free to use it in any way you wish.

The only thing I'd add is that I hope it is an opportunity for you to learn some ideas, rather than just copy something which you don't understand.
Thank you for your help :)
Topic archived. No new replies allowed.