Skip to content

Instantly share code, notes, and snippets.

@marsty5
Created August 29, 2017 15:09
Show Gist options
  • Save marsty5/ba4ba2027c805e68077853610c2520c5 to your computer and use it in GitHub Desktop.
Save marsty5/ba4ba2027c805e68077853610c2520c5 to your computer and use it in GitHub Desktop.
Print the max number of an array
## 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