# Comparing array elements

• 09-12-2003
Tride
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!
• 09-12-2003
Wledge
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); }```
• 09-12-2003
Dave_Sinkula
>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 */```
• 09-12-2003
Tride
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! :)
• 09-12-2003
DavT
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.
• 09-12-2003
Tride
Quote:

1) Step through your array 1 element at a time
Using a for loop, correct?

Quote:

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?
• 09-12-2003
DavT
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...
• 09-12-2003
Morgan
[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.
• 09-13-2003
Tride
You guys have been a big help. I understand it now :D. 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!