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.
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:
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:
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);
}
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);
}