11 Replies - 1264 Views - Last Post: 26 January 2017 - 03:01 AM Rate Topic: -----

#1 jstanley6  Icon User is offline

  • New D.I.C Head

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

Help with a reverse triangle code

Posted 23 January 2017 - 03:09 PM

Hello, I am trying to make my coding look a little better for this reverse triangle..here is what i have

size = 8
puts " " * (size - 1) + "*"
row = 0
while row < (size - 2) do
  puts " " * (size - row - 1) + "*" + " " * (row - 1) + "*"
  row += 1
end
puts "*" * size



also why can't i put the row increment above the puts " " * (size - row - 1) + "*" + " " * (row - 1) + "*" it keeps giving me an error when i try that and i can't see why.
If you can help that would be great,
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Help with a reverse triangle code

#2 jstanley6  Icon User is offline

  • New D.I.C Head

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

Re: Help with a reverse triangle code

Posted 23 January 2017 - 03:14 PM

View Postjstanley6, on 23 January 2017 - 03:09 PM, said:

Hello, I am trying to make my coding look a little better for this reverse triangle..here is what i have

size = 8
puts " " * (size - 1) + "*"
row = 0
while row < (size - 2) do
  puts " " * (size - row - 1) + "*" + " " * (row - 1) + "*"
  row += 1
end
puts "*" * size



also why can't i put the row increment above the puts " " * (size - row - 1) + "*" + " " * (row - 1) + "*" it keeps giving me an error when i try that and i can't see why.
If you can help that would be great,
Thanks!


I mean below it not above it, it works fine when i put it above it but when i put it below it it errors out
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,847
  • Joined: 12-June 08

Re: Help with a reverse triangle code

Posted 23 January 2017 - 03:18 PM

FYI - You don't need to quote the post above yours.

What errors are you getting?
Was This Post Helpful? 0
  • +
  • -

#4 jstanley6  Icon User is offline

  • New D.I.C Head

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

Re: Help with a reverse triangle code

Posted 23 January 2017 - 03:22 PM

Here is the code message when i try to add the counter below the puts for row

:in `*': negative argument (ArgumentError)
from C:/Users/RubymineProjects/Homework and Practise assignments/loops.rb:48:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
Was This Post Helpful? 0
  • +
  • -

#5 xclite  Icon User is offline

  • I wrote you an code
  • member icon


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

Re: Help with a reverse triangle code

Posted 23 January 2017 - 08:40 PM

You're trying to multiple a string by a negative number, which isn't possible. Think about what conditions would cause either `(size - row - 1)` to be negative or `(row - 1)`
Was This Post Helpful? 0
  • +
  • -

#6 jstanley6  Icon User is offline

  • New D.I.C Head

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

Re: Help with a reverse triangle code

Posted 23 January 2017 - 08:52 PM

Ok I tried to take the negative numbers out, the row += 1 worked now being underneath but it threw the alignment pretty off and I tried to fix it by adding to the strings and than just taking them completely out but it still didn't work. What am I doing wrong now?
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,765
  • Joined: 12-December 12

Re: Help with a reverse triangle code

Posted 24 January 2017 - 12:50 AM

Post your revised code.
Was This Post Helpful? 0
  • +
  • -

#8 jstanley6  Icon User is offline

  • New D.I.C Head

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

Re: Help with a reverse triangle code

Posted 24 January 2017 - 05:18 PM

size = 8
puts " " * (size - 1).to_i + "*"
row = 0
while row < (size - 2) do
  puts " " * (size - row  + 1).to_i + "*" + " " * (row + 1).to_i + "*"
  row += 1
end
puts "*" * size



Here is the revised code that I tried to use but this one is way off
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,765
  • Joined: 12-December 12

Re: Help with a reverse triangle code

Posted 25 January 2017 - 05:26 AM

You don't need to_i, the values are already integers. You just need to ensure that you don't request to multiply by a negative number.

I would have thought it should be possible to adjust the start and end values, and the expressions for the multipliers, so that a negative value isn't generated. It would help to get some graph paper and map out the steps and values that you need for each step.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,765
  • Joined: 12-December 12

Re: Help with a reverse triangle code

Posted 25 January 2017 - 05:35 AM

What is it, like this?

sssssss*
sssss*s*
ssss*ss*

You could possibly achieve it in a single loop, requiring values:

7, 1, 0, 0
5, 1, 1, 1
4, 1, 2, 1

but if this seems too complicated then adopt a similar analysis but skip the first line. Yes, it's easier (and less messy) if you handle the first line separately.
Was This Post Helpful? 0
  • +
  • -

#11 jstanley6  Icon User is offline

  • New D.I.C Head

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

Re: Help with a reverse triangle code

Posted 25 January 2017 - 11:26 AM

Ok, I am still a bit confused. Here is the original code that works fine and it draws the reverse right triangle like it is supposed to:
print "Enter the size of the triangle: "
size = gets.to_i
puts " " * (size - 1) + "*"
row = 0
while row < (size - 2) do
  row += 1
  puts " " * (size - row - 1) + "*" + " " * (row - 1) + "*"
end
puts "*" * size


I'm still not sure how I can switch that to look better and nicer.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,765
  • Joined: 12-December 12

Re: Help with a reverse triangle code

Posted 26 January 2017 - 03:01 AM

If you understand the code (and the earlier issue with a negative multiplier) and the code works, then it looks fine to me.

As mentioned, I think it is cleaner to have a separate statement for the first line (with a single star) rather than attempting to incorporate it in a single (more complicated) loop. Also, the remaining lines have four parts, so it requires a slightly cumbersome expression.

It is good that you appreciate the importance of refactoring but I would move on; refactoring will become more important when you have something meatier to work with.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1