3 Replies - 2155 Views - Last Post: 24 January 2013 - 08:13 PM Rate Topic: -----

#1 loopConfused  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 18-January 13

Error with loop code jumping out of loop

Posted 18 January 2013 - 10:31 AM

The program that I 'm making is to make a right triangle out of asterisk (*) from the user input wi9th spaces in the middle? So an example if someone where to input a number of 4 it would look like this
*
**
* *
****. The error that I'm having is when I input 3 it should look like this

* *
** **
*** but it looks like this ****. I think it is either the count or that I need to exit out of the loop.
Thanks for any explanations or assistance in this error.


print "Enter the size of triangle: "
#size = gets.to_i
size = gets.to_i
if size > 0
  puts "*"
  if size > 1
    puts "**"
    if size > 2
      if size > 3
        count = 1
        #makes the inside of the triangle
        while (count <= (size - 3))
          puts "*" + " " * (count) +("*")
          count += 1
        end
        count - 2
      else
        #size = 3
        count = 2
      end
      print "*" * (count + 2)
    end
  end
end


Is This A Good Question/Topic? 1
  • +

Replies To: Error with loop code jumping out of loop

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2142
  • View blog
  • Posts: 3,295
  • Joined: 21-June 11

Re: Error with loop code jumping out of loop

Posted 18 January 2013 - 10:41 AM

First of all your line 16 doesn't do anything. However I'm not sure what it's supposed to do and I believe that part of the code works fine without it, so that doesn't really seem to be a problem. You should probably just remove it.

The reason that you get 4 stars if size is 3 is because you set count to 2 in your else-block and 2+2 is 4. If you want 3 stars when size is 3, you should either set count to 1 or not use count for the last print at all - using size instead of count+2 makes more sense anyway and should do exactly what you want.
Was This Post Helpful? 4
  • +
  • -

#3 loopConfused  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 18-January 13

Re: Error with loop code jumping out of loop

Posted 22 January 2013 - 01:19 PM

View Postsepp2k, on 18 January 2013 - 10:41 AM, said:

First of all your line 16 doesn't do anything. However I'm not sure what it's supposed to do and I believe that part of the code works fine without it, so that doesn't really seem to be a problem. You should probably just remove it.

The reason that you get 4 stars if size is 3 is because you set count to 2 in your else-block and 2+2 is 4. If you want 3 stars when size is 3, you should either set count to 1 or not use count for the last print at all - using size instead of count+2 makes more sense anyway and should do exactly what you want.


Thank you very much for the assistance on this error. That little tidbit on the math was a great help in directing me in the right way to solve it on my own. I did have to play with it to figure it out, but I it did get solved.
Was This Post Helpful? 1
  • +
  • -

#4 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Error with loop code jumping out of loop

Posted 24 January 2013 - 08:13 PM

Hey, I don't usually spend too much time reading other peoples code but I liked how you provided enough information for others to 'play' and I found an app to invert screen colors and no I read code EVERYWHERE! I might have some thoughts you'd get something out of, regarding your code.

One of the more important conventions that most rubiests follow is the one that states you should break up your code into many smaller functions. When you're new to a language, that tip can be difficult to 'go along with'. As that I've been doing ruby and other langs for a while now, I've come to prefer smaller methods as that it becomes easier to see what your code is doing at a glance. So I went through your code and tried to break it up. I expected to be able to break it up into this basic algorithm as that it is the most accessible to my mind (perhaps it's the most straight forward way to approach the problem, but that stuff's always in the eye of the beholder):

// Render the top of the triangle

// Render the middle of the triangle

// Render the bottom of the triangle



The algorithm you used is not compatible with this algorithm. I believe you started fleshing out your algo by the following logic...

// 1)  Render the top of the triangle

// 2)  Render the next row of the triangle (what ever that might be)... 
//       -it will be the bottom if they gave input=2, 
//       -it will be the second line if they gave input=4 or more,
//       -it will be the entire middle of the triangle if they gave input=3

// 3)  etc.etc.



This kind of algorithm leads to lots of confusing if statements that can and should be refactored out. I remember taking a C++ course in college when I didn't really know what I was doing and I approached a calender problem dealing with months by writing 12 functions, one for each month. That was a yucky time for me and it took forever. In the end, I was only ever given full/near full marks for the assignment! He didn't mention anything about my algo! Luckily your problem isn't such a huge one, but the mess is still harder to parse than it needs to be and can be alleviated by adopting a different algorithm that supports a 3 stage attack to the problem (draw the top, draw the middle, draw the bottom).

Here's what I came up with. I'm sure you learned a lot in writing the code you did, and it might be a bit early in your career to be thinking about this aspect of programming, but thinking about the underlying algorithm might be a good exercise for you as well.

https://gist.github.com/4631410

def print_triangle(size)
  print_top_row_of_triangle(size)
  
  return if size <= 1

  print_middle_section_of_triangle(size)

  print_bottom_of_triangle(size)
end


def print_top_row_of_triangle(size)
  puts "*" if size >= 1
end

def print_middle_section_of_triangle(size)
  (size-2).times do |i|
    puts "*" + " "*i + "*"
  end
end

def print_bottom_of_triangle(size)
  puts "*" * size
end


print "Enter the size of triangle: "
size = gets.to_i
print_triangle(size)



I think if you look back on this a month or several months from now, you'll find your own code difficult to interpret and my broken up code to be pretty easy to figure out. Imo, that's the difference between good code and regular code.

This post has been edited by NotarySojac: 25 January 2013 - 12:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1