Question

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

Homework Answers

Answer #1

Below is the function in C++. In C++ when we pass array to a function we have to pass the size of the array also, there is no way in C++ for a function to determine how big the array is. So we also need to let the function know the array size unlike languages like Java or C# or python.

Here is the function in C++

===================================================================

int largest(int arr[], int size){
  
   int max_id = 0;
   for(int i=0; i<size; i++){
       if(arr[max_id]<arr[i]) max_id = i;
   }
   return arr[max_id];
}
===================================================================

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 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 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 program to determine the minimum element in an array of ten elements. The program...
Write a program to determine the minimum element in an array of ten elements. The program should have the following: 1. Class Name as ArrayProcessing. The main method should create an array of size 10 2. There should be two methods besides the main method in the class namely inputArray and MinimumElement 3. InputArray method should assign the ten elements in the array. Use scanner to input these elements. The array of 10 numbers in the method "InputArray" should be...
1) Develop a C++ function that determines the average value of an array of type double...
1) Develop a C++ function that determines the average value of an array of type double elements double GetAverage(double array[], int size) The function should accept as input an array of double values The function should accept as input the number of elements in the array of double values The function should return a double value which is the array's average value 2) Develop a C++ function that determines the variance of an array of type double elements double GetVariance(double...
Write a largestBelowValue(numbers, value) function that returns the largest number in the list numbers that is...
Write a largestBelowValue(numbers, value) function that returns the largest number in the list numbers that is smaller than value. Assume the numbers are always positive integers. Some example test cases (include these test cases in your program): >>>print(largestBelowValue([31, 5, 71, 53, 40, 17], 40)) 31 >>>print(largestBelowValue([31, 5, 71, 53, 40, 17], 41)) 40 returns None since no value is smaller than 2 in the list >>>print(largestBelowValue([31, 5, 71, 53, 40, 17], 2)) None
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
Complete following function which receives an array of integers and the length of the array, and...
Complete following function which receives an array of integers and the length of the array, and then returns the sum of all the positive numbers in the array. For example, if an array that is passed to this function contains following numbers: -1, 2, 0, 3, 4, -3, 0, 2, 0, and then the return value of the function should be 11. Will this function be working correctly? Yes or No? int sumPositive(int a[],int length) { int s=0;     for(int...
Write an application in Java which includes an algorithm that takes an array of any size,...
Write an application in Java which includes an algorithm that takes an array of any size, selects the high and low integer from the array of integers with each pass and builds a new array of integers by inserting the high and low selection with each pass. Your output should show the original array of integers and the output of each pass on a new line. Note: You can assume all integers are positive, but your code must work for...
(C++) Write a function called triple that takes an array of integers as a parameter as...
(C++) Write a function called triple that takes an array of integers as a parameter as well as the length of the array. It should triple each value in the array. The function should not return anything. (Note that the contents of the array WILL be modified.)
Write a function called odd_rms that returns orms, which is the square root of the mean...
Write a function called odd_rms that returns orms, which is the square root of the mean of the squares of the first nn positive odd integers, where nn is a positive integer and is the only input argument. For example, if nn is 3, your function needs to compute and return the square root of the average of the numbers 1, 9, and 25. You may use built-in functions including, for example, sum and sqrt, except for the built-in function...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT