943,910 Members | Top Members by Rank

Ad:
  • Ruby Discussion Thread
  • Unsolved
  • Views: 2246
  • Ruby RSS
Apr 20th, 2009
0

for loops (101)

Expand Post »
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:

Ruby Syntax (Toggle Plain Text)
  1. Enter number of rows: 5
  2. ....*
  3. ...**
  4. ..***
  5. .****
  6. *****

It seemed to take me ages to get my head around the structure of for loops and I ended up firstly using while loops just to get the program working. After that I eventually got this for loop version working.

ruby Syntax (Toggle Plain Text)
  1. # nestedforloop.rb
  2. # 19 Apr, 2009.
  3. print "Enter the number of rows: "
  4. rows = gets.chomp!
  5. rows = rows.to_i
  6.  
  7. for r in (1..rows)
  8. for c in (1..(rows - r))
  9. print "."
  10. end
  11. r == rows ? c = 1 : c += 1
  12. for c in (c..rows)
  13. print "*"
  14. end
  15. print "\n"
  16. end
The above works but I'm thinking maybe there was a way I didn't need to use the r == rows ? c = 1 : c += 1 line, as that wasn't necessary under the C++ for loop/code. Should I stop thinking in terms of how traditional for loops are structured? I'm led to believe that for is in fact an alias (if that's the right word) of the each method. Any hints re - using the each method would be appreciated (as applied to the above).
Similar Threads
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Apr 20th, 2009
0

Re: for loops (101)

Here are some suggestions I've since received from other sources. In essence there is always more than one way to skin a cat.

ruby Syntax (Toggle Plain Text)
  1. for r in (1..rows)
  2. for c in (1..(rows - r))
  3. print "."
  4. end
  5. for c in ((rows - r)...rows) # improvement here, notice 3 dots
  6. print "*"
  7. end
  8. print "\n"
  9. end

Here's another solution and not as many lines of code required.

ruby Syntax (Toggle Plain Text)
  1. print "Enter the number of rows: "
  2. rows = gets.strip.to_i
  3.  
  4. rows.times do |row|
  5. print '.' * (rows-row-1)
  6. print '*' * (row+1)
  7. puts ''
  8. end
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Ruby Forum Timeline: FTP - Download multiple files using ruby
Next Thread in Ruby Forum Timeline: hi





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC