How to find a maximum element of the array by using C programming?
How to find a maximum element of the array by using C programming?
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.
#include<stdio.h>
int main(){
int a[20],num,i;
printf("Enter the size of array :\n");
scanf("%d",&num);
printf("Enter the element of array\n");
for(i=0;i<num;i++){
scanf("%d",&a[i]);
}
for(i=0;i<num;i++){
if(a[0]<a[i]){
a[0]=a[i];
}
}
printf("Maximum element is :%d\n",a[0]);
}
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.
Comments
Post a Comment