for loops (101)

Reply

Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

for loops (101)

 
0
  #1
Apr 20th, 2009
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:

  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.

  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).
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: for loops (101)

 
0
  #2
Apr 20th, 2009
Here are some suggestions I've since received from other sources. In essence there is always more than one way to skin a cat.

  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.

  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Ruby Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC