>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
*/