C program to find maximum element in array

This code find maximum or largest element present in an array. It also prints the location or index at which maximum element occurs in array. This can also be done by using pointers (see both codes). The algorithm to find maximum is first we assume that maximum element occurs at beginning of 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 maximum value and index at which it occurs is updated. Similarly we can find minimum element in an array.

C programming code

#include <stdio.h>
 
int main()
{
  int array[100], maximum, size, c, location = 1;
 
  printf("Enter the number of elements in array\n");
  scanf("%d", &size);
 
  printf("Enter %d integers\n", size);
 
  for (c = 0; c < size; c++)
    scanf("%d", &array[c]);
 
  maximum = array[0];
 
  for (c = 1; c < size; c++)
  {
    if (array[c] > maximum)
    {
       maximum  = array[c];
       location = c+1;
    }
  }
 
  printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
  return 0;
}
If maximum occurs two or more times times in array then index at which it occurs first is printed or maximum value at smallest index. You can easily modify this code this code to print largest index at which maximum occur. You can also store all indices at which maximum occur in an array.
 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.
#include <stdio.h>
 
int find_maximum(int[], int); 
 
int main() {
  int c, array[100], size, location, maximum;
 
  printf("Input number of elements in array\n");
  scanf("%d", &size);
 
  printf("Enter %d integers\n", size);
 
  for (c = 0; c < size; c++)
    scanf("%d", &array[c]);
 
  location = find_maximum(array, size);
  maximum  = array[location];
 
  printf("Maximum element location = %d and value = %d.\n", location + 1, maximum);
  return 0;
}
 
int find_maximum(int a[], int n) {
  int c, max, index;
 
  max = a[0];
  index = 0;
 
  for (c = 1; c < n; c++) {
    if (a[c] > max) {
       index = c;
       max = a[c];
    }
  }
 
  return index;
}

C programming code using pointers

#include <stdio.h>
 
int main()
{
  long array[100], *maximum, size, c, location = 1;
 
  printf("Enter the number of elements in array\n");
  scanf("%ld", &size);
 
  printf("Enter %ld integers\n", size);
 
  for ( c = 0 ; c < size ; c++ )
    scanf("%ld", &array[c]);
 
  maximum  = array;
  *maximum = *array;
 
  for (c = 1; c < size; c++)
  {
    if (*(array+c) > *maximum)
    {
       *maximum = *(array+c);
       location = c+1;
    }
  }
 
  printf("Maximum element found at location %ld and it's value is %ld.\n", location, *maximum);
  return 0;
}
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.

No comments:

Post a Comment