Created
August 29, 2017 15:09
-
-
Save marsty5/ba4ba2027c805e68077853610c2520c5 to your computer and use it in GitHub Desktop.
Print the max number of an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Problem | |
# Ask the user for 5 numbers (integers) | |
# and print the maximum/biggest number. | |
# | |
# Use an array to save the 5 numbers. | |
################################################# | |
## First Step - Break down the problem in steps | |
# 1a. Create an empty array of size 5 and store it in a variable | |
# Remember, everything is case-sensitive | |
# 1b. Create a counter with value 0 and store it in a variable | |
# 2. Start loop (loop do) | |
# 2a. Ask for a number (puts) (loop=0) | |
# 2b. Save the number in the array in position 0 (gets, to_i) | |
# 2d. Break the loop when (if) counter is (==) 4 (break) | |
# 2c. Increase the counter by 1 | |
# 2e. Close the loop (end) | |
# 3. Find the biggest number | |
# 4. Print the biggest number | |
################################################# | |
## Second Step - Translate each step in code | |
array_of_numbers = Array.new(5) | |
counter = 0 | |
loop do | |
puts("Give me a number por favor:") | |
array_of_numbers[counter] = gets.chomp.to_i | |
break if counter == 4 | |
counter = counter + 1 | |
end | |
max_number = array_of_numbers.max | |
puts("Max number is: #{max_number}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment