### Thread: Can someone help i don't understand what went wrong

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2007
Posts
8
Rep Power
0

#### Can someone help i don't understand what went wrong

Code:
```#include <stdio.h>
int main(void)

{

int i; //number loop 1 - number of house(s)
int j; //initial array
int k; //for array usage
int m; //initial array for loop 2 - calculation
int n; //for array usage
int p; //for loop 3 - finding best price
int q, r; //for finding best house
float houseInt[j]; //array for house initial
float fuelAnnual[j]; //array for fuel annual
float taxRate[j]; //array for tax rate
float total[m]; //array for total price
float max = total[0];
float min = total[0];

printf("Enter the the number of house(s): ");
scanf("%d",&i);

//input house detail using loop
for ( j = 0; j < i; j++ )
{
int k = j+1;
printf("Enter the initial house cost for house number %2d: ", k);
scanf("%f",&houseInt[j]);

printf("Enter the Annual Fuel Cost for house number %2d: ", k);
scanf("%f",&fuelAnnual[j]);

printf("Enter the taxRate for house house number %2d: ", k);
scanf("%f",&taxRate[j]);

}
printf("Total cost of a house after a five-year period: \n");
for ( m = 0; m < i; m++ )
{
int n = m+1;
total[m] = houseInt[m] + fuelAnnual[m]*5 + houseInt[m]*taxRate[m]*5; //the total price formula
printf("house %d = %.2f\n", n, total[m]); //list of house price

}
//to find the best price
for (p = 0; p < i; p++)
{
if (total[p] > max)
{
max = total[p];
}
else if (total[p] < min)
{
min = total[p]; //we only use this
}
}

//find the house with the best price using comparing method with min value and array of house
for (q=0; q < i; q++)
{
if (min == total[q])
r = q+1;
}

printf ("The best price is : %.2f which is house number %d ", min, r);  //final answer :)

return 0;

}```
this error keeps coming up
.c: In function 'main':
.c:10:9: warning: unused variable 'n' [-Wunused-variable]

.c:8:9: warning: unused variable 'k' [-Wunused-variable]

.c:16:5: warning: 'm' is used uninitialized in this function [-Wuninitialized]
.c:13:5: warning: 'j' is used uninitialized in this function [-Wuninitialized]
2. Those aren't errors. They are warnings. They are telling you that you have declared two variables you aren't using, and two you are using without initializing.
Last edited by bullet; November 15th, 2013 at 08:19 AM.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2007
Posts
8
Rep Power
0
Originally Posted by bullet
Those aren't errors. They are warnings. They are telling you that you have declared two variables you aren't using, and two you are using without initializing.
how can i rectify this?... i'm already stuck
4. Originally Posted by gagakzs
how can i rectify this?... i'm already stuck
You can remove any variables you don't need.

Also initialize the others.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2007
Posts
8
Rep Power
0
Originally Posted by bullet
You can remove any variables you don't need.

Also initialize the others.
when you say it only shows warning does it mean that the code is actually correct?
6. Originally Posted by gagakzs
when you say it only shows warning does it mean that the code is actually correct?
Technically yes, but you shouldn't have these two lines

Code:
```float taxRate[j]; //array for tax rate
float total[m]; //array for total price```
You should initialize the variables before you use them.
7. Originally Posted by gagakzs
when you say it only shows warning does it mean that the code is actually correct?
No, your code is not correct.

C was created by super-programmers for their own use in writing an operating system, UNIX. As a result, C compilers operate on the assumption that you, the programmer, know what you are doing.

Originally Posted by Doug Gwyn
UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things.
Similarly, C is not designed to stop its users from doing stupid things, such as using uninitialized variables and pointers, since its designers didn't want it to stop them from doing clever things.

Remember in all creative endeavors that at first you learn rules of how to do everything and after you have gained sufficient mastery the art then your most creative work involves breaking the rules. The secret to that is that before you can bend or break the rules, you really need to know what you are doing.

Since you have not yet reached that point, then for you warnings mean that your program is not correct.
8. #### crisis!

Code:
```  int j; //initial array
int m; //initial array for loop 2 - calculation```
j and m have unknown values, what ever bit patterns were left over in the stack memory from whatever code happened to write there last. Do not use uninitialized data.
Code:
```  float houseInt[j]; //array for house initial
float fuelAnnual[j]; //array for fuel annual
float taxRate[j]; //array for tax rate
float total[m]; //array for total price```
total is an array of unknown size and unknown values. Initialized data before using it.
Code:
```  float max = total[0];
float min = total[0];```