Thread: finding minimum in array

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    13

    finding minimum in array

    i have an array (d_available[i])with some zero elements and non zero elements i want to find the minimum of the non zero elements (min_available)

    this is what i thought will work but still gives me 0 as minimium
    Code:
    min_available=d_available[0] ;
    for(i=0;i<NROWS;i++){
         if (min_available>d_available[i] && d_available> 0)
                    min_available=d_available[i];
     
                        }

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Test for the minimum, and if one is found, add a number onto an accumulator.
    The world is waiting. I must leave you now.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    7
    I believe i see the problem. You have this:
    Code:
    min_available=d_available[0] ;
    for(i=0;i<NROWS;i++){
         if (min_available>d_available[i] && d_available> 0)
                    min_available=d_available[i];
     
                        }
    It should be this:

    Code:
    min_available=d_available[0] ;
    for(i=0;i<NROWS;i++){
         if (min_available>d_available[i] && d_available[i]> 0)
                    min_available=d_available[i];
     
                        }
    Does this work?
    -Chris

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    min_available=d_available[0] ;

    What if this already is 0 ?

    C# Code would look something like this probably:

    PHP Code:
    using System;

    namespace 
    ConsoleApplication2
    {
        class 
    Class1
        
    {
            [
    STAThread]
            static 
    void Main(string[] args)
            {
                
    int[] array = new int[10];
                
    int min int.MaxValue;
                
    bool found false;

                foreach( 
    int i in array )
                {
                    if( 
    && min )
                    {
                        
    found true;
                        
    min i;
                    }
                }

                if( ! 
    found 
                {
                    
    Console.WriteLine"No nonzero values in array" );
                }
                else
                {
                    
    Console.WriteLine"The minimum nonzero value was {0}"min );
                }
            }
        }

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Finding a hyphen in an array.
    By omnificient in forum C Programming
    Replies: 7
    Last Post: 06-17-2008, 02:31 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM