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
[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.
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...
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...
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...
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
Write a method name maxElement, which returns the largest value in an array that is passed...
Write a method name maxElement, which returns the largest value in an array that is passed as an argument. The method must use recursion to find the largest element. Demonstrate the method in a program. Write the JAVA CODE on the paper that is provided. (Put your name on every piece of paper) Use the following array to test the method. Int [] numbers = {2,12,1999,99,100,4,7,300} PROGRAMMING LANGUAGE- JAVA. Please answer the question ASAP. Thanks in advance!
Write a function that accepts an int array arr, the array’s size sz and integer n,...
Write a function that accepts an int array arr, the array’s size sz and integer n, the function should create a new array that it’s n times the array arr. The fucntion copy the content of arr to the new array n times. The Example arr = {5, 2, 1} n=3 Newarr = {5, 2, 1, 5, 2, 1, 5, 2, 1} (c++)
• First, create a function called addNumber, which has a formal parameter for an array of...
• First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
In Python Compose a function that takes an array of strictly positive floats as its parameter...
In Python Compose a function that takes an array of strictly positive floats as its parameter and rescales the array so that each element is between 0.0 and 1.0 by subtracting the minimum value from each element and then dividing each element by the difference between the minimum and maximum values in the array. The first constraint in this assignment is that you must use a list comprehension to implement the function. To test your function, have your program call...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT