11 Replies - 1097 Views - Last Post: 22 January 2017 - 03:31 PM Rate Topic: -----

#1 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Regarding loops

Posted 22 January 2017 - 12:30 PM

Hello. I am trying to code this program that will loop something like this
1
12
123
1234
12345
123456
1234567
12345678
and so on depending on what the number the user has entered.
Here is what I have so far:
print "Enter the size of the triangle: "
size = gets.to_i
row = 1
 while row <= 5
  counter = 1
 while counter <= 5
  print counter
  counter += 1
 end
  puts
  row += 1
  end


that code will print out 12345, 5 times, I just need to add a few things to make it print out numbers entered from 0-N

This post has been edited by xclite: 22 January 2017 - 12:33 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Regarding loops

#2 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,028
  • Joined: 12-May 09

Re: Regarding loops

Posted 22 January 2017 - 12:34 PM

Have you thought about which line you need to change? You should be able to deduce based on which loop does which part of the printing.
Was This Post Helpful? 0
  • +
  • -

#3 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Regarding loops

Posted 22 January 2017 - 12:40 PM

View Postxclite, on 22 January 2017 - 12:34 PM, said:

Have you thought about which line you need to change? You should be able to deduce based on which loop does which part of the printing.

Yes, I have been thinking of this for a couple days already. I know I have to change the 5's of course to make the program be able to run infinite numbers. Like if the user entered a 20 it would print like that all the way until it reached 20. I just haven't been able to find good tutorials that would show me what to change it to and how i can break it down into lines. I'm a beginner of Ruby, so it might look easy to you but I don't understand it yet.
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,028
  • Joined: 12-May 09

Re: Regarding loops

Posted 22 January 2017 - 12:53 PM

Ok, so you need to change the fives. Lets walk through what your loops do.

The loop on line 4 prints your rows. Right now, it always prints 5 rows. How many rows should it print?

Your inner loop on line 6 prints the numbers in the row. Right now, it always prints the numbers one through five. It needs to print an increasing sequence of numbers, up to the user's input. There is a relationship between how many times this loop should execute and which row we are on, right?
Was This Post Helpful? 0
  • +
  • -

#5 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Regarding loops

Posted 22 January 2017 - 01:05 PM

View Postxclite, on 22 January 2017 - 12:53 PM, said:

Ok, so you need to change the fives. Lets walk through what your loops do.

The loop on line 4 prints your rows. Right now, it always prints 5 rows. How many rows should it print?

Your inner loop on line 6 prints the numbers in the row. Right now, it always prints the numbers one through five. It needs to print an increasing sequence of numbers, up to the user's input. There is a relationship between how many times this loop should execute and which row we are on, right?


Ok, that is very helpful and I was playing around with them and almost made the program crash. would i just add == row or == counter to keep the counter going up to whatever number the user input says to go up to?
Was This Post Helpful? 0
  • +
  • -

#6 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,028
  • Joined: 12-May 09

Re: Regarding loops

Posted 22 January 2017 - 01:09 PM

Well, kind of. Let's think about this logically. You don't need to try random things. Your outer loop prints each row. You ask the user for a number, which determines how many rows you should print...
Was This Post Helpful? 0
  • +
  • -

#7 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Regarding loops

Posted 22 January 2017 - 01:10 PM

View Postjstanley6, on 22 January 2017 - 01:05 PM, said:

View Postxclite, on 22 January 2017 - 12:53 PM, said:

Ok, so you need to change the fives. Lets walk through what your loops do.

The loop on line 4 prints your rows. Right now, it always prints 5 rows. How many rows should it print?

Your inner loop on line 6 prints the numbers in the row. Right now, it always prints the numbers one through five. It needs to print an increasing sequence of numbers, up to the user's input. There is a relationship between how many times this loop should execute and which row we are on, right?


Ok, that is very helpful and I was playing around with them and almost made the program crash. would i just add == row or == counter to keep the counter going up to whatever number the user input says to go up to?


I think I have that part down now actually, I was thinking about it too hard I guess, here is what I have now:
row = 1
 while row <= size
  counter = 1
 while counter <= size
  print counter
  counter += 1
 end
  puts
  row += 1
  end


Now i just need to figure out how to get them on different lines

This post has been edited by xclite: 22 January 2017 - 01:11 PM
Reason for edit:: Fixing code tags.

Was This Post Helpful? 0
  • +
  • -

#8 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,028
  • Joined: 12-May 09

Re: Regarding loops

Posted 22 January 2017 - 01:12 PM

So right now, what you *probably* have as output is something like:

12345678
12345678
12345678
12345678
12345678
12345678
12345678
12345678

?

Your loop on line 4 always prints everything. But how much it prints depends on what row you're in. You helpfully are already tracking what row you're in.
Was This Post Helpful? 0
  • +
  • -

#9 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Regarding loops

Posted 22 January 2017 - 01:16 PM

Yes, that is what I can see now, I can see how the row does depend on what you type in but I can't see how to change that yet, do I need another loop surrounding that?

This post has been edited by andrewsw: 22 January 2017 - 02:32 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#10 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,028
  • Joined: 12-May 09

Re: Regarding loops

Posted 22 January 2017 - 02:21 PM

Your inner loop goes from 1 to size, and needs to change to 1 from... what? To some number that represents the row you're in.
Was This Post Helpful? 2
  • +
  • -

#11 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Regarding loops

Posted 22 January 2017 - 03:24 PM

Oh wow, I got it working now.

Thanks for all the great help on this. And I thank you for not giving me the answer but instead helped me to understand it. Without that help I wouldn't of understood the coding.

Thanks again!
Was This Post Helpful? 1
  • +
  • -

#12 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,028
  • Joined: 12-May 09

Re: Regarding loops

Posted 22 January 2017 - 03:31 PM

Glad you got it figured out :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1