Thread: Finding the minimum value in an array?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    26

    Finding the minimum value in an array?

    Is there an easy way to find a minimum value in an array? Thanks.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Certainly,

    1. Declare a variable (foo) to store the minimum.
    2. foo = arr[0]
    3. for every element in arr, if less than foo, assign foo to new value.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    int min=0;
    
    
    for (int i=0;i<iSizeOfArray;i++)
    {
      if (i==0) 
         min=Array[i];
      else
        if (Array[i]<min) min=Array[i];
       
    }

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Thanks, sorry for not providing an example in C in the first place, I unrealistically expected them to try to learn things for themselves.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    Thanks a lot. But I have another question. Is there a way to make it so that all the numbers will print out from smallest to largest? All that gives me is the smallest number every time.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Sort the array, then print it.
    Search the board for lots of examples.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    I checked, but still do not understand how to sort the array. Most examples have the array with numbers already entered, but in my program, the user enters up to 10 numbers, or presses 0 to quit. Any help? Here is my code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define Max_numbers 10
    
    
    
    
    int main(void)
    
    {        
             
             double x[Max_numbers],
                    sum = 0,
                    average;
                 int y;   
             int i;
             int step = 1;
             char str[256];
             
             printf("Created by Matt Torsiello\n\n");
    
        printf( "Please enter your name:\t " );
        gets(str);
        printf("Hello, %s!\n", str);
        printf("Please Enter %d numbers separated by returns\n",
                       Max_numbers);
        for (i = 0; i < Max_numbers; ++i) {
            
           scanf("%lf", &x[i]); 
            if (x[i] == 0) {
                     y = i;
            i +=10; 
            step = 2; }
    } 
    
    
     if (step == 2) {
              
              printf ("You entered %d numbers.\n", y);
              
          for (i = 0; i < y; ++i) {
              
              printf("\n%.2f", x[i]);    
              
              sum += x[i]; }
    
    }
    
    printf("\n\nThe total is %.2f.", sum);
    average = sum / y;
    printf("\nThe average is %.2f.", average);
    
    
    
    
    system("Pause");
    return (0);
    
    
    }

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Read in your numbers like this:

    Code:
        for (count = 0; count < MAX; count++)
        {
        	printf ("Enter an integer - 0 to quit: ");
        	scanf  ("%d", &temp);
        
        	if (temp != 0)
        		numbers [count] = temp;
        
        	else break;
        }
    then use a bubble sort like the one here

    just change the looping condition to outer < count instead of 9.
    That should then sort the array, for as many entries as you have
    entered.

    EDIT: Use fgets for reading in your string!!!!!!!!!!!!!
    Last edited by Richie T; 02-18-2006 at 12:04 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by cwr
    Thanks, sorry for not providing an example in C in the first place, I unrealistically expected them to try to learn things for themselves.
    \/
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

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