Two positive numbers

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3539 Views - Last Post: 22 February 2012 - 07:49 PM Rate Topic: -----

#1 whamp  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 16-February 12

Two positive numbers

Posted 16 February 2012 - 07:12 AM

Write a Ruby program that asks a user to enter two positive numbers ( > 0). The program returns the larger value that is in the range 10...20 inclusive, or return 0 if neither is in that range.

What I have so far is:

int main()
	{
	    int num1, num2;
	 
	    //get two numbers
	    cout << "Please enter two positive integers seperated by a comma: ";
	    cin >> num1 >> num2;
	    while ((num1 < 0) || (num2 < 0))
	    {
	        cout << "Please enter two positive integers: ";
	        cin >> num1 >> num2;
	    }



I believe this is how you get the user to enter two positive numbers but I don't know how to get Ruby to return the highest value 10 .... 20 or return 0 if not in range.

I would very thankful for any help.

Thank you.

This post has been edited by Karel-Lodewijk: 16 February 2012 - 07:53 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Two positive numbers

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,182
  • Joined: 21-June 11

Re: Two positive numbers

Posted 16 February 2012 - 07:35 AM

What you have so far is C++ code...
Was This Post Helpful? 1
  • +
  • -

#3 whamp  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 16-February 12

Re: Two positive numbers

Posted 16 February 2012 - 07:39 AM

So I'm not even on the right track?
Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Two positive numbers

Posted 16 February 2012 - 07:54 AM

View Postwhamp, on 16 February 2012 - 02:39 PM, said:

So I'm not even on the right track?


Depends, do you want to write a C++ and did you post in the wrong forum or did you want to write a Ruby program ? In ruby you might read 2 comma separated numbers like this:

#reads 2 comma separated numbers
num1, num2 = gets.chomp.split(",").map{|x| x.to_i}



In short gets reads a string ("11,2\n"), chomp removes the newline (enter character) ("11,2"), split creates an Array by seperating the string at every comma (["11","2"]). The map with to_i converts the two numbers to integers ([11,2]) and in Ruby you can assign an array of 2, to two variables (num1 = 11, num2 = 2).

As you can see the language is totally different

This post has been edited by Karel-Lodewijk: 16 February 2012 - 08:03 AM

Was This Post Helpful? 1
  • +
  • -

#5 whamp  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 16-February 12

Re: Two positive numbers

Posted 16 February 2012 - 07:58 AM

I need to write a Ruby program.
Was This Post Helpful? 0
  • +
  • -

#6 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Two positive numbers

Posted 16 February 2012 - 08:00 AM

So start with Ruby code.
Was This Post Helpful? 0
  • +
  • -

#7 whamp  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 16-February 12

Re: Two positive numbers

Posted 16 February 2012 - 08:27 AM

View PostKarel-Lodewijk, on 16 February 2012 - 07:54 AM, said:

View Postwhamp, on 16 February 2012 - 02:39 PM, said:

So I'm not even on the right track?


Depends, do you want to write a C++ and did you post in the wrong forum or did you want to write a Ruby program ? In ruby you might read 2 comma separated numbers like this:

#reads 2 comma separated numbers
num1, num2 = gets.chomp.split(",").map{|x| x.to_i}



In short gets reads a string ("11,2\n"), chomp removes the newline (enter character) ("11,2"), split creates an Array by seperating the string at every comma (["11","2"]). The map with to_i converts the two numbers to integers ([11,2]) and in Ruby you can assign an array of 2, to two variables (num1 = 11, num2 = 2).

As you can see the language is totally different


Thanks Karel-Lodewijk

So I need to start with:

#reads 2 comma separated numbers
num1, num2 = gets.chomp.split(",").map{|x| x.to_i}

This is my first dealings with Ruby so I'm getting really confused.
Was This Post Helpful? 0
  • +
  • -

#8 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Two positive numbers

Posted 16 February 2012 - 08:29 AM

Do you have a plan for solving the problem to begin with? An algorithm? What steps would you take to determine what number to return?
Was This Post Helpful? 0
  • +
  • -

#9 whamp  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 16-February 12

Re: Two positive numbers

Posted 16 February 2012 - 08:30 AM

View Postxclite, on 16 February 2012 - 08:00 AM, said:

So start with Ruby code.



xclite

I realize you may know everything there is to know about programming, but I don't which is why I asked a question. Please don't try to insult me or waste anyone's time with useless comments. Everyone has to learn from the beginning at some point & I'm not the only one that needs help.
Was This Post Helpful? -3
  • +
  • -

#10 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Two positive numbers

Posted 16 February 2012 - 08:37 AM

This is the first step to problem solving. Before you start writing code and running into road blocks, you need to have a plan. If you don't know how to decide which number should be returned, how are you going to tell a computer how to do it?
Was This Post Helpful? 0
  • +
  • -

#11 whamp  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 16-February 12

Re: Two positive numbers

Posted 16 February 2012 - 09:47 AM

View Postxclite, on 16 February 2012 - 08:37 AM, said:

This is the first step to problem solving. Before you start writing code and running into road blocks, you need to have a plan. If you don't know how to decide which number should be returned, how are you going to tell a computer how to do it?



This is what I have now, but it's still not working:
class Screen
  def cls
  end
end

print "enter number1:"
num = num.gets.to_i
 if num1 < 0 then print "enter a positive number"
 else
   if num1 < 9 and num1 > 21 then 
   print "enter a number in range 10-20"
 end
 
print "enter number2:"
num2 = num2.gets.to_i
 if num2 < 0 then print "enter a positive number"
 else
   if num2 < 9 and num2 > 21 then 
   print "enter a number in range 10-20"
 end
 if num1 > num2 then
      print "the larger number is #{num1}"
 else
   print "the larger number is #(num2}"
      
 end
 Console_Screen.cls


This post has been edited by xclite: 16 February 2012 - 09:48 AM
Reason for edit:: Please use code tags!

Was This Post Helpful? 0
  • +
  • -

#12 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Two positive numbers

Posted 16 February 2012 - 09:51 AM

Let's talk about your first issue - your program is supposed to take any input > 0. Why are you asking for a new number if it's between 10 and 20? That's part of the later calculation. Also, your condition wouldn't work for that because a number cannot be < 9 and > 21 at the same time.
Was This Post Helpful? 0
  • +
  • -

#13 whamp  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 16-February 12

Re: Two positive numbers

Posted 16 February 2012 - 09:55 AM

View Postxclite, on 16 February 2012 - 09:51 AM, said:

Let's talk about your first issue - your program is supposed to take any input > 0. Why are you asking for a new number if it's between 10 and 20? That's part of the later calculation. Also, your condition wouldn't work for that because a number cannot be < 9 and > 21 at the same time.



I did < 9 > 21 because the range has to be 10 to 20.
Was This Post Helpful? 0
  • +
  • -

#14 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Two positive numbers

Posted 16 February 2012 - 09:57 AM

But the range does not have to be between 10 and 20. The problem says to return 0 if the numbers are out of that range, not reject the input.

And the < 9 and > 21 still doesn't work for a couple reasons. First, you want <= and >=, because 9 and 21 would both be invalid. Second, you want an OR, not an AND.
Was This Post Helpful? 0
  • +
  • -

#15 abhaydchamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-February 12

Re: Two positive numbers

Posted 22 February 2012 - 04:16 AM

Similar to answer by Karel-Lodewijk with samall difference:

num1, num2 = gets.chomp.split(",").collect{|x| x.to_i}
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2