Question

Define a function to find the largest element from an integer array.

Define a function to find the largest element from an integer array.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function that returns the largest element of an array? Your function should accept a...
Write a function that returns the largest element of an array? Your function should accept a 1-D array as an input and return the largest number. You may assume all numbers are integers. CODE IN C++ PLEASE
using MATLAB Write a function which will take two inputs: an array ? and an integer...
using MATLAB Write a function which will take two inputs: an array ? and an integer ?. Here, 1 <= ? < ?????h(?). The objective is to find a contiguous subarray of length ? with the largest sum, within the array ?. The output of the function will be that largest sum. For example, x = [1 2 -1 3], and k =2. Then there are 3 possible contiguous subarrays. 1st subarray : [ 1 2], sum = 3 2nd...
[C++ Language] Write a function to find out the average of an integer array and return...
[C++ Language] Write a function to find out the average of an integer array and return to the main function.
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
An array is sorted (in ascending order) if each element of the array is less than...
An array is sorted (in ascending order) if each element of the array is less than or equal to the next element. An array of size 0 or 1 is sorted Compare the first two elements of the array; if they are out of order, the array is not sorted; otherwise, check the if the rest of the array is sorted. Write a boolean-valued method named isSorted that accepts an integer array, and the number of elements in the array...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
1. Define a function frame_with_ones(n) that takes an integer as an argument. Your function will create...
1. Define a function frame_with_ones(n) that takes an integer as an argument. Your function will create nXn arrays of zeros and then “frame” it with a border of ones. Return the formatted array. The returned array will have the shape: (n+2)x(n+2) HINT: Slicing may be beneficial to solving this. For example: frame_with_ones(3) returns: np.array( [[ 1. 1. 1. 1. 1. ] [ 1. 0. 0. 0. 1. ] [ 1. 0. 0. 0. 1. ] [ 1. 0. 0. 0....