You are here

C program to find minimum element in array

C program to find minimum or the smallest element in an array. It also prints the location or index at which the minimum element occurs in the array. Our algorithm assumes the first element as minimum and then compare it with other elements if an element is smaller than it then it becomes the new minimum, and this process is repeated till complete array is scanned.

C programming code

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int array[100], minimum, size, c, location = 1;
  6.    
  7.     printf("Enter number of elements in array\n");
  8.     scanf("%d", &size);
  9.    
  10.     printf("Enter %d integers\n", size);
  11.    
  12.     for (c = 0; c < size; c++)
  13.         scanf("%d", &array[c]);
  14.    
  15.     minimum = array[0];
  16.    
  17.     for (c = 1; c < size; c++)
  18.     {
  19.         if (array[c] < minimum)
  20.         {
  21.            minimum = array[c];
  22.            location = c+1;
  23.         }
  24.     }
  25.    
  26.     printf("Minimum element is present at location %d and it's value is %d.\n", location, minimum);
  27.     return 0;
  28. }

Output of program:
Minimum element in array program

If minimum occurs two or more times in the array then the index at which it occurs first is printed or minimum value at the smallest index. You can modify the code to print the largest index at which minimum occurs. You can also store all indices at which minimum occurs in the array.

Download Minimum element in array program.

C programming code to find minimum using function

Our function returns index at which minimum element occur.

  1. #include <stdio.h>
  2.  
  3. int find_minimum(int[], int);
  4.  
  5. int main() {
  6.   int c, array[100], size, location, minimum;
  7.  
  8.   printf("Input number of elements in array\n");
  9.   scanf("%d", &size);
  10.  
  11.   printf("Input %d integers\n", size);
  12.  
  13.   for (c = 0; c < size; c++)
  14.     scanf("%d", &array[c]);
  15.  
  16.   location = find_minimum(array, size);
  17.   minimum  = array[location];
  18.  
  19.   printf("Minimum element location = %d and value = %d.\n", location + 1, minimum);
  20.   return 0;
  21. }
  22.  
  23. int find_minimum(int a[], int n) {
  24.   int c, min, index;
  25.  
  26.   min = a[0];
  27.   index = 0;
  28.  
  29.   for (c = 1; c < n; c++) {
  30.     if (a[c] < min) {
  31.        index = c;
  32.        min = a[c];
  33.     }
  34.   }
  35.  
  36.   return index;
  37. }

C programming code using pointers

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int array[100], *minimum, size, c, location = 1;
  6.    
  7.     printf("Enter the number of elements in array\n");
  8.     scanf("%d", &size);
  9.    
  10.     printf("Enter %d integers\n", size);
  11.    
  12.     for (c = 0; c < size; c++)
  13.         scanf("%d", &array[c]);
  14.    
  15.     minimum = array;
  16.     *minimum = *array;
  17.    
  18.     for (c = 1; c < size; c++)
  19.     {
  20.         if (*(array+c) < *minimum)
  21.         {
  22.            *minimum = *(array+c);
  23.            location = c+1;
  24.         }
  25.     }
  26.    
  27.     printf("Minimum element found at location %d and it's value is %d.\n", location, *minimum);
  28.     return 0;
  29. }