C Program to Find Largest Element from Array (User Defined Function)


Question: Write a program in C to read an array containing n elements and find the largest element from an array using user defined function.

Largest Element from Array Using User Defined Function


#include<stdio.h>

/* Function prototype */
float large(float a[100], int n);

void main()
{
 float a[100], res;
 int i, n;
 clrscr();
 printf("Enter n:\n");
 scanf("%d", &n);
 /* Reading array */
 for(i=0;i< n;i++)
 {
  printf("a[%d]=",i);
  scanf("%f", &a[i]);
 }
 /* Function Call */
 res = large(a,n);
 /* Displaying result */
 printf("Large = %f", res);
 
 return 0;
}
/* Function definition for large */
float large(float a[10], int n)
{
 float lg = a[0];
 int i;
 for(i=0;i< n;i++)
 {
  if(a[i]>lg)
  {
   lg = a[i];
  }
 }
 return(lg);
}