Monday, October 31, 2011

Finding the Minimum and Maximum Element of an Array in ANSI C

If we want to determine the minimum element we will need a variable to retain it. This variable will be compared to all of the array's elements. If one element is less than our temporary minimum, he will become the new minimum and all the other remaining elements will be compared to this new minimum.
/*
 * Description:
 *  Returns the smallest element from the array
 * Parameters:
 * array - a pointer to an integer array
 * size - the size of the array
 * Returns:
 *  The smallest element of the array
 */
int GetMin(int *array, int size)
{
   int i;
   int min = array[0];
   for(i=0;i<size;i++)
   {
      if(min<array[i])
      {
         min = array[i];
      }
   }
   return min;
}
The same logic applies when we want to determine the maximum element of the array:
/*
 * Description:
 *  Returns the largest element from the array
 * Parameters:
 * array - a pointer to an integer array
 * size - the size of the array
 * Returns:
 *  The largest element of the array
 */
int GetMax(int *array, int size)
{
   int i;
   int max = array[0];
   for(i=0;i<size;i++)
   {
      if(max>array[i])
      {
         max = array[i];
      }
   }
   return max;
}
If we would want to return the index of the minimum element, respectively the maximum element, we would need an extra variable to hold the index. The implementations are:
/*
 * Description:
 *  Returns the index of the smallest element from the array
 * Parameters:
 * array - a pointer to an integer array
 * size - the size of the array
 * Returns:
 *  The index of the smallest element of the array
 */
int GetMinElementIndex(int *array, int size)
{
   int i;
   int min = array[0];
   int minIndex = 0;
   for(i=0;i<size;i++)
   {
      if(min<array[i])
      {
         min = array[i];
         minIndex = i;
      }
   }
   return minIndex;
}
/*
 * Description:
 *  Returns the index of the largest element from the array
 * Parameters:
 * array - a pointer to an integer array
 * size - the size of the array
 * Returns:
 *  The index of the largest element of the array
 */
int GetMaxElementIndex(int *array, int size)
{
   int i;
   int max = array[0];
   int maxIndex = 0;
   for(i=0;i<size;i++)
   {
      if(max>array[i])
      {
         max = array[i];
         maxIndex = i;
      }
   }
   return maxIndex;
}
Example:
#include<stdio.h>

int GetMin(int array[], int size);
int GetMax(int array[], int size);
int GetMinElementIndex(int array[], int size);
int GetMaxElementIndex(int array[], int size);

int main(void)
{
   /*We shall declare an array and populate it with 10 elements*/
   int array[10] = {1,2,3,4,-5,6,-1,2,-3};
   /*Searching for the minimum element*/
   int minElement = GetMin(array,10);
   /*Searching for the maximum element*/
   int maxElement = GetMax(array,10);
   /*Searching for the index of the minimum element*/
   int minElementIndex = GetMinElementIndex(array,10);
   /*Searching for the index of the maximum element*/
   int maxElementIndex = GetMaxElementIndex(array,10);
   /*Printing the results*/
   printf("Minimum element = %d at the index %d\n"
          "Maximum element = %d at the index %d\n",
           minElement,minElementIndex, maxElement,maxElementIndex);
   return 0;
}
/*Output
Minimum element = 6 at the index 5
Maximum element = -5 at the index 4
 */

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger