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
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];
}
===================================================================
Get Answers For Free
Most questions answered within 1 hours.