SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: for loops (101)
-
Apr 19, 2009, 22:50 #1
- Join Date
- Jun 2007
- Location
- Melbourne, Australia.
- Posts
- 121
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
for loops (101)
In learning Ruby I've dug out some old C++ exercises and one in particular which can be found here deals with a for loop. I'm basically converting the C++ code to Ruby code. I won't repeat the C++ code but the exercise is as follows:
Write a C++ program that asks the user to enter a number of rows to be printed. It should then display for the first row one asterisk preceded by periods. The second row should display two asterisks preceded by periods and so on until all the rows have been printed as entered by the user.
A sample run would be like so:
Code:Enter number of rows: 5 ....* ...** ..*** .**** *****
Code:# nestedforloop.rb # 19 Apr, 2009. print "Enter the number of rows: " rows = gets.chomp! rows = rows.to_i for r in (1..rows) for c in (1..(rows - r)) print "." end r == rows ? c = 1 : c += 1 for c in (c..rows) print "*" end print "\n" end
-
Apr 20, 2009, 01:34 #2
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
for is an alias of each.
Code:for a in b # do stuff end
Code:b.each do |a| #do stuff end
http://www.ruby-doc.org/docs/Program...ssions.html#UK
-
Apr 20, 2009, 04:30 #3
- Join Date
- Jun 2007
- Location
- Melbourne, Australia.
- Posts
- 121
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks Reggie. I transposed my for loop example to the each way of doing things and it wouldn't run, initially. The program kept stalling on the r == rows ? c = 1 : c += 1 line. Finally I determined it was a scoping issue for the c variable. Initializing the c variable prior to the looping solved the problem. Is probably easier showing the code in full.
Code:print "Enter the number of rows: " rows = gets.chomp! rows = rows.to_i c = 1 # had to initialize here re - scoping (could be any integer value) (1..rows).each do |r| (1..(rows - r)).each do |c| print "." end r == rows ? c = 1 : c += 1 # if c not initilized above, is out of scope here. (c..rows).each do |c| print "*" end print "\n" end
-
Apr 20, 2009, 04:56 #4
- Join Date
- Mar 2008
- Location
- Berchtesgaden, Germany
- Posts
- 32
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hehe, tricky one but I solved it by using (rows-r) instead of c:
Code:for r in (1..rows) for c in (1..(rows - r)) print "." end for c in ((rows - r)...rows) print "*" end print "\n" end
-
Apr 20, 2009, 05:12 #5
- Join Date
- Jun 2007
- Location
- Melbourne, Australia.
- Posts
- 121
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Apr 20, 2009, 05:15 #6
- Join Date
- Apr 2009
- Location
- Australia
- Posts
- 8
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Here's another possible solution and an example of the expressiveness of Ruby.
Code:print "Enter the number of rows: " rows = gets.strip.to_i rows.times do |row| print '.' * (rows-row-1) print '*' * (row+1) puts '' end
-
Apr 20, 2009, 05:31 #7
- Join Date
- Jun 2007
- Location
- Melbourne, Australia.
- Posts
- 121
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Bookmarks