-3

I have a homework assignment that I need to finish. I think most of the code is working but I am having trouble with the last part. I need to display the largest number that a user enters (into an array). Below is the code I have so far. I am open to any suggestions. Thanks in advance.

Here's the description of the assignment: Write a Ruby application that allows a user to input a series of 10 integers and determines and prints the largest integer. Your program should use at least the following three variables: a) counter: A counter to count to 10 (i.e., to keep track of how many numbers have been input and to determine when all 10 numbers have been processed). b) number: The integer most recently input by the user. c) largest: The largest number found so far.

class Screen
  def cls
    puts ("\n")
    puts "\a"
  end
def pause
   STDIN.gets
 end
end

class Script
  def display_instructions 
      Console_Screen.cls
      print "This script will take the user input of 10 integers and then 
      will 
      print the largest."
      print "\n\nPress enter to continue."
      Console_Screen.cls
      Console_Screen.pause
   end

  def getNumber #accepts user input
    list = Array.new
    10.times do
      Console_Screen.cls
      print "This script accepts 10 integers."
      print "\n\nPlease type an integer and press enter."
      input = STDIN.gets
      input.chop!
      list.push(input)
     end
   end

  def display_largest(number) #displays the largest integer entered by the 
    user
    Console_Screen.cls
    print "The largest integer is " + 
  end

 def runScript
    number = getNumber
    Console_Screen.cls
    display_largest(number)
 end
end

 #Main Script Logic
 Console_Screen = Screen.new
 LargestNum = Script.new
 answer = "" 
 loop do
   Console_Screen.cls
   print "Are you ready to start the script? (y/n): "
   print "\n\nWould you like instructions on how this script works? (h): "
   answer = STDIN.gets
   answer.chop!
   break if answer =~ /y|n|h/i
 end

 if answer == "h" or answer == "H"
  LargestNum.display_instructions
  print "Are you ready to start the script? (y/n): "
  answer = STDIN.gets
  answer.chop!
  end

 if answer == "n" or answer == "N"
  Console_Screen.cls
  puts "Okay, maybe another time.\n\n"
  Console_Screen.pause
 else
  loop do
    LargestNum.runScript
    print "\n\nEnter Q to quit or press any key to run the script again: "
    runAgain = STDIN.gets
    runAgain.chop!
    break if runAgain =~ /Q/i
  end
end
  • I mean, even if there wasn't a built-in function for this, it seems like it's a fairly simple matter to iterate and set the current max, no? Have you tried *anything* yet? – Dave Newton Apr 10 '18 at 17:14
  • a) You don't need a counter, just use `times`. b) Why do you need the last number input? c) Use `max`. – Sagar Pandya Apr 10 '18 at 17:29
  • "I think most of the code is working" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Apr 10 '18 at 19:28
  • What does it mean that you "think" it is working? Does it work or doesn't it work? Also, please make sure that you provide a [mcve] that demonstrates your problem. In particular, your example fails the *minimal* part; I am 100% sure that it does not require 70 lines to demonstrate your problem, in fact, I am willing to bet it can be done in 2. – Jörg W Mittag Apr 10 '18 at 19:31
  • I apologize for my vagueness. I mean that I believe that the code is accepting the input and adding it to the array. My issue is being able to display the largest number that was input by the user. I have been trying to figure out how to basically display the information that the user has input into the script and output it into a string to display back to the user. – Chase Ritchey Apr 10 '18 at 20:02

2 Answers2

9

This question has been asked and answered so many times before. Personally I think, as this answer suggests, the built in .max is the best solution.

[1, 3, 5].max #=> 5
bork
  • 1,556
  • 4
  • 21
  • 42
-1

Have you learned about for loops yet? You have to iterate through the array. For a very trivial example, you can do something like

max = 0
for element in list 
    if element > max
        max= element
return max
chevybow
  • 9,959
  • 6
  • 24
  • 39