Comparing array elements

This is a discussion on Comparing array elements within the C Programming forums, part of the General Programming Boards category; The book I'm reading says to make a program that shows the difference between the largest and smallest elements of ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    49

    Comparing array elements

    The book I'm reading says to make a program that shows the difference between the largest and smallest elements of an array. It sounded easy and I thought I'd be able to do it, but man, I've been trying for awhile and I can't think of any possible way to do this. Is there something obvious here that I'm missing? I've been using a loop to go through the array and another loop to go through at the same time for comparison, but this brings up lots of problems.

    Can someone give me sample code and explain how to do this? It would help greatly!

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int array[] = {-1,3,4,8,4,30,-3,0};
    
    	int maxEl,minEl,i;
    	maxEl = minEl = array[0];
    	for(i = 1;i < 8;i++)
    	{
    		if(array[i] > maxEl)
    			maxEl = array[i];
    		if(array[i] < minEl)
    			minEl = array[i];
    	}
    
    	printf("Diff: %d\n",maxEl - minEl);
    }

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Can someone give me sample code and explain how to do this?
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
       int array[]  = { 7, 42, -9, 0, 137, 6, -2, -5, 3, 1 };
       int smallest = INT_MAX;
       int largest  = INT_MIN;
       size_t i, is = 0, il = 0;
       for ( i = 0; i < sizeof(array)/sizeof(*array); ++i )
       {
          if ( array[i] > largest )
          {
             largest = array[i];  /* capture new largest value */
             il = i;              /* capture index of value */
          }
          else if ( array[i] < smallest )
          {
             smallest = array[i]; /* capture new smallest value */
             is = i;              /* capture index of value */
          }
       }
       printf("largest    = %3d (array[%lu])\n", largest,  (long unsigned)il);
       printf("smallest   = %3d (array[%lu])\n", smallest, (long unsigned)is);
       printf("difference = %d\n", largest - smallest);
       return 0;
    }
    
    /* my output
    largest    = 137 (array[4])
    smallest   =  -9 (array[2])
    difference = 146
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    49
    Hmmm.. Both codes contain some things that I'm not familiar with. I only semi-understand what's going on in them. Maybe I should just go through my book again.

    Thanks anyway, guys!

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    1) Step through your array 1 element at a time

    2) for each element decide if this is the biggest element you have seen so far. if it is then save it in an appropriate variable.

    3) also decide if this is the smallest element you have seen so far. if it is save it in an appropriate variable.

    4) once you've looked at all the elements of the array take away the minimum value you saw from the maximum value you saw. that is the answer you wanted.

    If you are still have problems post the code you have written and people will help you correct it.
    DavT
    -----------------------------------------------

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    49
    1) Step through your array 1 element at a time
    Using a for loop, correct?

    2) for each element decide if this is the biggest element you have seen so far. if it is then save it in an appropriate variable.
    Maybe this is my problem; what do I compare the element to to decide that it is the biggest so far?

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Take a look at WLedges code 'cos its slightly easier to understand than Dave's (although the basics behind it are the same)

    Code:
    #include <stdio.h>
    
    int main()
    {
      // this just sets up your array and declares the 
      // variables you are going to use
      int array[] = {-1,3,4,8,4,30,-3,0};
      int maxEl,minEl,i;
    
      // the first element in the array is referenced by array[0]
      // it is both the biggest and smallest number we have
      // seen so far... (p.s. array indexes start at 0)
      maxEl = array[0];
      minEl = array[0];
    
      // use a for loop to step through the array one element
      // at a time (notice we start at i = 1 which is the 2nd
      // element in the array)
      for(i = 1;i < 8;i++)
      {
        // is array[i] bigger than the biggest value we 
        // have seen so far?
        if(array[i] > maxEl)
          // if yes, save it as the new biggest so far
          maxEl = array[i];
    
        // is array[i] smaller than the smallest value
        // we have seen so far?
        if(array[i] < minEl)
          // if yes, save it as the new smallest so far
          minEl = array[i];
      } // repeat until we get to the end of the array
    
      // calculate the difference (maxEl - minEl) 
      // and print to screen
      printf("Diff: %d\n",maxEl - minEl);
    }
    hope that helps...
    Last edited by DavT; 09-12-2003 at 12:08 PM.
    DavT
    -----------------------------------------------

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    [qoute]
    Maybe this is my problem; what do I compare the element to to decide that it is the biggest so far?
    [/quote]

    Basically anything! All you have to do is start with something small in your maximum variable, and something large in your minimum variable.

    While your stepping through the array, if the number is smaller than the number in the minimum variable, set the minimum variable to equal the array element, and the same for the maximum variable.

    Dave_S has gone for the INT_MAX and INT_MIN values, which are defined as the biggest and smallest values which your machine can store in an integer variable, and are therefore the perfect starting values.
    There is no such thing as a humble opinion

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    49
    You guys have been a big help. I understand it now . It seems so simple---I can't believe I didn't know how to do it!

    Well, you've all saved me a lot of trouble. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 06:37 AM
  2. Shifting elements in an Array
    By mmarab in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 12:11 PM
  3. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-25-2004, 12:24 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:37 PM

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