variable being used without Initializing

For the following function why is it that the error keep stating that my year and sector have been used without initialized. For the int a it represents the year and char b represents the sector.


void displayGDP(int a, char b)
{
int year;
int sector;
float GDP;
if(a==2011)
a=0;

if(a==2012)
a=1;

if(a==2013)
a=2;

if(a==2014)
a=3;

if((b=='m')&&(a==0))
GDP=dataGDP[0][0];

if((b=='m')&&(a==1))
GDP=dataGDP[0][1];

if((b=='m')&&(a==2))
GDP=dataGDP[0][2];

if((b=='m')&&(a==3))
GDP=dataGDP[0][3];

if((b=='c')&&(a==0))
GDP=dataGDP[1][0];

if((b=='c')&&(a==1))
GDP=dataGDP[1][1];

if((b=='c')&&(a==2))
GDP=dataGDP[1][2];

if((b=='c')&&(a==3))
GDP=dataGDP[1][3];

if((b=='u')&&(a==0))
GDP=dataGDP[2][0];

if((b=='u')&&(a==1))
GDP=dataGDP[2][1];

if((b=='u')&&(a==2))
GDP=dataGDP[2][2];

if((b=='u')&&(a==3))
GDP=dataGDP[2][3];

if((b=='w')&&(a==0))
GDP=dataGDP[3][0];

if((b=='w')&&(a==1))
GDP=dataGDP[3][1];

if((b=='w')&&(a==2))
GDP=dataGDP[3][2];

if((b=='w')&&(a==3))
GDP=dataGDP[3][3];

if((b=='t')&&(a==0))
GDP=dataGDP[4][0];

if((b=='t')&&(a==1))
GDP=dataGDP[4][1];

if((b=='t')&&(a==2))
GDP=dataGDP[4][2];

if((b=='t')&&(a==3))
GDP=dataGDP[4][3];

printf("the GDP for the selected year and sector is %.2f%d%c",GDP,year,sector);
}

In the above code the variables year and sector are defined - that is memory has been allocated for the variables, But at no place in the code is any particular value assigned to them. Thus they contain garbage.

However - the printf statement uses the codes %d %c (integer and character) to print the values, but year and sector are both integers. There a mismatch.

On the other hand, the function parameters
a and b
 
void displayGDP(int a, char b)
are indeed of type int and char.

A simple fix is to delete the variables year and sector and change the printf() to print a and b instead.

For readability you might use your code editor to change all instances of variable "a" to "year", and "b" to "sector" - but be careful or you could end up with something horrible like
"dataGDP" changed to "dyeartyearGDP" which is definitely not what you want.
Hi thanks for your help here but after changing the codes it still says year and sector not initialized. sorry for the trouble

void displayGDP(int a, char b)
{
int year;
char sector;
float GDP;
if(year==2011)
year==0;

if(year==2012)
year==1;

if(year==2013)
year==2;

if(year==2014)
year==3;

if((sector=='m')&&(year==0))
GDP=dataGDP[0][0];

if((sector=='m')&&(year==1))
GDP=dataGDP[0][1];

if((sector=='m')&&(year==2))
GDP=dataGDP[0][2];

if((sector=='m')&&(year==3))
GDP=dataGDP[0][3];

if((sector=='c')&&(year==0))
GDP=dataGDP[1][0];

if((sector=='c')&&(year==1))
GDP=dataGDP[1][1];

if((sector=='c')&&(year==2))
GDP=dataGDP[1][2];

if((sector=='c')&&(year==3))
GDP=dataGDP[1][3];

if((sector=='u')&&(year==0))
GDP=dataGDP[2][0];

if((sector=='u')&&(year==1))
GDP=dataGDP[2][1];

if((sector=='u')&&(year==2))
GDP=dataGDP[2][2];

if((sector=='u')&&(year==3))
GDP=dataGDP[2][3];

if((sector=='w')&&(year==0))
GDP=dataGDP[3][0];

if((sector=='w')&&(year==1))
GDP=dataGDP[3][1];

if((sector='w')&&(year==2))
GDP=dataGDP[3][2];

if((sector=='w')&&(year==3))
GDP=dataGDP[3][3];

if((sector=='t')&&(year==0))
GDP=dataGDP[4][0];

if((sector=='t')&&(year==1))
GDP=dataGDP[4][1];

if((sector=='t')&&(year==2))
GDP=dataGDP[4][2];

if((sector=='t')&&(year==3))
GDP=dataGDP[4][3];

printf("The GDP for the selected year and sector is %.2f %d %c",GDP,a,b);
}

Well, I actually said to delete the variables year and sector, which you haven't done.

However - I misread the code too. Because in the first few lines, the value of the year (e.g. 2014) is changed to some other value (3), then you still do need to keep the variable a. But make it a separate variable, not the one declared in the function parameter list.
First version:
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
78
79
80
81
void displayGDP(int year, char sector)
{
    int a = 0;     // initialise with default value
    
    float GDP = 9; // initialise with default value
    
    // replace value of a according to the year
    if (year==2011)
        a=0;

    if (year==2012)
        a=1;

    if (year==2013)
        a=2;

    if (year==2014)
        a=3;

    if ((sector=='m')&&(a==0))
        GDP=dataGDP[0][0];

    if ((sector=='m')&&(a==1))
        GDP=dataGDP[0][1];

    if ((sector=='m')&&(a==2))
        GDP=dataGDP[0][2];

    if ((sector=='m')&&(a==3))
        GDP=dataGDP[0][3];

    if ((sector=='c')&&(a==0))
        GDP=dataGDP[1][0];

    if ((sector=='c')&&(a==1))
        GDP=dataGDP[1][1];

    if ((sector=='c')&&(a==2))
        GDP=dataGDP[1][2];

    if ((sector=='c')&&(a==3))
        GDP=dataGDP[1][3];

    if ((sector=='u')&&(a==0))
        GDP=dataGDP[2][0];

    if ((sector=='u')&&(a==1))
        GDP=dataGDP[2][1];

    if ((sector=='u')&&(a==2))
        GDP=dataGDP[2][2];

    if ((sector=='u')&&(a==3))
        GDP=dataGDP[2][3];

    if ((sector=='w')&&(a==0))
        GDP=dataGDP[3][0];

    if ((sector=='w')&&(a==1))
        GDP=dataGDP[3][1];

    if ((sector=='w')&&(a==2))
        GDP=dataGDP[3][2];

    if ((sector=='w')&&(a==3))
        GDP=dataGDP[3][3];

    if ((sector=='t')&&(a==0))
        GDP=dataGDP[4][0];

    if ((sector=='t')&&(a==1))
        GDP=dataGDP[4][1];

    if ((sector=='t')&&(a==2))
        GDP=dataGDP[4][2];

    if ((sector=='t')&&(a==3))
        GDP=dataGDP[4][3];

    printf("the GDP for the selected year and sector is %.2f %d %c",GDP,year,sector);
}


But there's a great deal of repetition in that code. It can be simplified:
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
void displayGDP(int year, char sector)
{
    int a = 0;     // initialise with default value
    
    // replace value of a according to the year
    if (year==2011)
        a=0;

    if (year==2012)
        a=1;

    if (year==2013)
        a=2;

    if (year==2014)
        a=3;
        
    int x = 0; // initialise with default value
    
    // replace value of x according to the sector
    if (sector=='m')
        x = 0;
    
    if (sector=='c')
        x = 1;    
    
    if (sector=='u')        
        x = 2;
    
    if (sector=='w')
        x = 3;
    
    if (sector=='t')
        x = 4;    
        
    float GDP = dataGDP[x][a];        
        
    printf("the GDP for the selected year and sector is %.2f %d %c",GDP,year,sector);
}
Last edited on
Or use a switch-case:
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
void displayGDP(int year, char sector)
{
    int a = 0;     // initialise with default value
    
    // replace value of a according to the year
    switch (year)
    {
        case 2011: a = 0; break;
        case 2012: a = 1; break;
        case 2013: a = 2; break;
        case 2014: a = 3; break;
    }
        
    int x = 0; // initialise with default value
    
    // replace value of x according to the sector
    switch (sector)
    {
        case 'm': x = 0; break;
        case 'c': x = 1; break;
        case 'u': x = 2; break;
        case 'w': x = 3; break;
        case 't': x = 4; break;
    }        
        
    float GDP = dataGDP[x][a];        
        
    printf("the GDP for the selected year and sector is %.2f %d %c",GDP,year,sector);
}
that's a lot of If statements it would be easier and more readable to use a switch statement like Chervil pointed out.
Topic archived. No new replies allowed.