Find maximum of elements in Array

This example will show you how to find a maximum of elements in an Array using C program.

The below findMax function takes two arguments and returns an integer

int findMax(int *a, int n)

*a – pointer to an array

n – size of the array

Example

#include<stdio.h>

int findMax(int *a, int n){
	int max,i;
	max = a[0];
	for(i=1;i<n;i++){
		if(a[i]>max){
			max = a[i];
		}
	}
	return max;
}

main(){
	int i;
	int size=5;
	int a[5];
	int max;
	for(i=0;i<size;i++){
		a[i]=i+100;
	}
	max = findMax(a,size);
	printf("Maximum value in array is %d",max);
	return;
}

Output

Maximum value in array is 104

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *