1. Min Max not working

Code:
```void priceAvg(struct products *allProducts, int numProducts, int numMonths)
{
int i, j, k, l, m, n;
double overall[numProducts], avg[numProducts], min[numProducts], max[numProducts];

for(i=0; i<numProducts; i++)
{
for(j=0; j<numMonths; j++)
{
overall[i] += allProducts[i].price[j];
}
avg[i] = overall[i] / numMonths;

for(k=0; k<numMonths; k++)
{
for(l=0; l<numMonths; l++)
{
if(allProducts[i].price[k] < allProducts[i].price[l])
min[i] = allProducts[i].price[k];
}
}

for(m=0; m<numMonths; m++)
{
for(n=0; n<numMonths; n++)
{
if(allProducts[i].price[m] > allProducts[i].price[n])
max[i] = allProducts[i].price[m];
}
}
printf("\n%s     Avg:\$%.2f    Min:\$%.2f    Max:\$%.2f", allProducts[i].name, avg[i], min[i], max[i]);
}
}```
The averages work fine, but for some reason the min and max doesn't work all the time

2. You should be able to use a single loop to calculate min and max together and that loop shouldn't need to be a nested loop:

Code:
```max[i] = min[i] = allProducts[i].price[0];
for(j=1; j<numMonths; j++)
{
if( allProducts[i].price[j] < min[i] )
min[i] = allProducts[i].price[j];
if( allProducts[i].price[j] > max[i] )
max[i] = allProducts[i].price[j];
}```
You can then get rid of the k, l, m, and n variables.

You don't need to initialize the "overall" array to 0 first? Is it something about using VLA's in a C99 compiler?[/edit]

3. Thanks, that worked, ummm. I guess not, my compiler says "variable-sized object may not be initialized" when i try to set it to 0...I might be retarded, this is the first C program I've done in 3 years.

4. You'll have to run through a loop then setting your array values to zero.

Quzah.

5. Originally Posted by quzah
You'll have to run through a loop then setting your array values to zero.
in C89
Code:
```double arr1[100]; <<-- uninitialized array
double arr2[100] ={0}; <<-- initialized array, all with zeros
double arr3[100] ={2,3}; <<-- initialized array, with {2,3} and zeros..```
With shouldn't this happen in C99 ??

6. Did you just not read the previous posts, or what?

Originally Posted by B0bDole
Thanks, that worked, ummm. I guess not, my compiler says "variable-sized object may not be initialized" when i try to set it to 0...I might be retarded, this is the first C program I've done in 3 years.

Quzah.