Define a function to find the largest element from an integer array.
PROGRAMS ARE DONE WITH C,C++,JAVA AND PYTHON PROGRAMMING LANGUAGE
HERE THE FULL PROGRAM WITH FUNCTION DEFINITION IS SOLVED.
PROGRAM WITH C
#include<stdio.h>
/* define function large_array to find largest element of the array
*/
/* it has two parameters arary content and array length */
int large_array(int arr[],int n)
{
int i=0,m=0;
/* traverse the array */
for(i=0;i<n;i++)
{
if(m<arr[i])
{
m=arr[i];
}
}
/* return largest element */
return m;
}
int main()
{
/* arr is array of integers */
int arr[100],n,i,lrg;
/* console input number of values */
printf("How many elements : ");
scanf("%d",&n);
/* console input values of array*/
printf("\nEnter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
/* large_array function called and returned value
stored in lrg */
lrg=large_array(arr,n);
/* print lrg */
printf("\nLargest element is : %d ",lrg);
return 0;
}
SCREEN SHOT
OUTPUT
PROGRAM WITH C++
#include<iostream>
using namespace std;
/* define function large_array to find largest element of the array
*/
/* it has two parameters arary content and array length */
int large_array(int arr[],int n)
{
int i=0,m=0;
/* traverse the array */
for(i=0;i<n;i++)
{
if(m<arr[i])
{
m=arr[i];
}
}
/* return largest element */
return m;
}
int main()
{
/* arr is array of integers */
int arr[100],n,i,lrg;
/* console input number of values */
cout<<"How many elements : ";
cin>>n;
/* console input values of array*/
printf("\nEnter the elements\n");
for(i=0;i<n;i++)
{
cin>>arr[i];
}
/* large_array function called and returned value
stored in lrg */
lrg=large_array(arr,n);
/* print lrg */
cout<<endl<<"Largest element is :
"<<lrg;
return 0;
}
SCREEN SHOT
OUTPUT
PROGRAM WITH JAVA
/* import scanner class with util package */
import java.util.Scanner;
class largest
{
/* define function large_array to find largest element of the array
*/
/* it has two parameters arary content and array length */
int large_array(int arr[],int n)
{
int i=0,m=0;
/* traverse the array */
for(i=0;i<n;i++)
{
if(m<arr[i])
{
m=arr[i];
}
}
/* return largest element */
return m;
}
}
public class largest_array
{
public static void main(String[] args)
{
int n,i,lrg;
largest ob=new largest();
Scanner sc=new Scanner(System.in);
/* console input number of values */
System.out.print("How many elements : ");
n=sc.nextInt();
/* arr is array of integers */
int arr[]=new int[n];
/* console input values of array*/
System.out.println("\nEnter the elements\n");
for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
/* large_array function called with object ob and
returned value stored in lrg */
lrg=ob.large_array(arr,n);
/* print lrg */
System.out.println("Largest element is : " + lrg);
}
}
SCREEN SHOT
OUTPUT
PROGRAM WITH PYTHON ( IF ANY PROBLEM FACED IN INDENTATION KINDLY FOLLOW THE BELOW SCREEN SHOT. IT IS IN IMAGE FORMAT , SO INDENTATION IS ACCURATE, HERE IN PYTHON IN PLACE OF ARRAY LIST IS TAKEN )
#define function large_array to find largest element of the
list
# it has two parameters list content and list length
def largest_array(arr,n):
max=0
for i in range(0,n):
if(arr[i]>max):
max=arr[i]
# return max
return max
#initialize the list
lst=[]
#console input number of values
n=int(input('How many elements : '))
#console input values of list
print('Enter elements ')
for i in range (0,n):
val=int(input())
lst.insert(i,val)
#large_array function called and returned value stored in lrg
lrg= largest_array(lst,n)
# print lrg
print('Largest element is : ', lrg)
SCREEN SHOT
OUTPUT
Get Answers For Free
Most questions answered within 1 hours.