Min Max not working

This is a discussion on Min Max not working within the C Programming forums, part of the General Programming Boards category; Code: void priceAvg(struct products *allProducts, int numProducts, int numMonths) { int i, j, k, l, m, n; double overall[numProducts], avg[numProducts], ...

  1. #1
    Bob Dole for '08 B0bDole's Avatar
    Join Date
    Sep 2004
    Posts
    618

    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
    Hmm

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    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.

    [edit]You don't need to initialize the "overall" array to 0 first? Is it something about using VLA's in a C99 compiler?[/edit]
    Last edited by hk_mp5kpdw; 06-09-2005 at 07:35 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Bob Dole for '08 B0bDole's Avatar
    Join Date
    Sep 2004
    Posts
    618
    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.
    Hmm

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll have to run through a loop then setting your array values to zero.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote 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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you just not read the previous posts, or what?

    Quote 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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Max Min
    By jhwebster1 in forum C Programming
    Replies: 6
    Last Post: 02-16-2006, 01:17 PM
  2. Random Number Range Problem.
    By xamlit in forum C Programming
    Replies: 11
    Last Post: 01-26-2006, 11:55 AM
  3. Max & Min value and refine histogram
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-20-2002, 08:04 AM
  4. Min and Max Macros
    By Batman.......hehehehe in forum C++ Programming
    Replies: 2
    Last Post: 07-09-2002, 01:14 PM
  5. Min, Max field entry?
    By JCCC in forum C Programming
    Replies: 1
    Last Post: 04-16-2002, 07:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21