w3resource

Ruby Array Exercises: Find the largest value from a given array of integers of odd length

Ruby Array: Exercise-26 with Solution

Write a Ruby program to find the largest value from a given array of integers of odd length. The array length will be a least 1.

Ruby Array Exercises: Find the largest value from a given array of integers of odd length

Ruby Code:

def check_array(nums)
    max = nums[0];
	if(max <= nums[nums.length-1])
		max = nums[nums.length-1]
	end
	if(max <= nums[nums.length/2])
		max = nums[nums.length/2]
	end
	return max;
end

print check_array([1, 3, 4]),"\n"
print check_array([1, 2, 7]),"\n"
print check_array([1, 2])  

Output:

4
7
2

Flowchart:

Flowchart: Find the largest value from a given array of integers of odd length

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to create a new array length 3 containing the elements from the middle of an given array of integers of odd length (at least 3).
Next: Write a Ruby program to create a new array using first three elements of an given array of integers. If the length of the given array is less than three return the original array.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/ruby-exercises/array/ruby-array-exercise-26.php