You are here

C program to find maximum element in an array

C program to find the maximum or the largest element present in an array. It also prints the location or index at which maximum element occurs in the array. The algorithm to find maximum is: first we assume that maximum element occurs at the beginning of the array and stores that value in a variable. Then we compare it with other array elements one by one, if any element is greater than our assumed maximum, then the maximum value and the index at which it occurs are updated. Similarly, we can find minimum element in an array.

C programming code

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int array[100], maximum, 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.   maximum = array[0];
  16.  
  17.   for (c = 1; c < size; c++)
  18.   {
  19.     if (array[c] > maximum)
  20.     {
  21.        maximum  = array[c];
  22.        location = c+1;
  23.     }
  24.   }
  25.  
  26.   printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
  27.   return 0;
  28. }

If the maximum element occurs, two or more times in array then index at which it occurs first is printed or maximum value at the smallest index. You can easily modify this code this code to print the largest index at which maximum occurs. You can also store all indices at which maximum occur in an array.

Download Maximum element in array program.

Output of program:
Maximum element in array c program

C programming code to find maximum using function

Our function returns index at which maximum element occur.

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

C programming code using pointers

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

The complexity of above code is O(n) as the time used depends on the size of input array or in other words time to find maximum increases linearly as array size grows.