Horizontal Stars

Z

Zunaster

This is the code I have been trying to execute. But this seems
incorrect. I have trying to make horizontal bar of stars (*) .
In this program you the program generates how many times the
horizontal line of stars to generated and then it makes horizontal
lines comprising of number of stars you stated.

puts " Please enter a number "
s = gets()
while x<s
i=rand(6)
print i
x+=1
while j<i
puts("*")
j+=1
end
end
 
R

Rob Biedenharn

This is the code I have been trying to execute. But this seems
incorrect. I have trying to make horizontal bar of stars (*) .
In this program you the program generates how many times the
horizontal line of stars to generated and then it makes horizontal
lines comprising of number of stars you stated.

puts " Please enter a number "
s = gets()
while x<s
i=rand(6)
print i
x+=1
while j<i
puts("*")
j+=1
end
end

While this rings like a homework assignment, I'll answer somewhat
crypticly:
puts "*" * s }
How many? 19
*******************
*******************
*******************
=> 3puts "*" * s }
How many? 7
*******
*******
*******
*******
=> 4

Note that gets() returns a String (see: ri String)
String#to_i converts a string to an integer
"x" * 2 is the same as "xx"
The idiomatic of writing a loop to execute a fixed number of times
(say, 3) is 3.times {|c| block }

You never initialize 'j'

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
L

Lincoln Anderson

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
This is the code I have been trying to execute. But this seems
incorrect. I have trying to make horizontal bar of stars (*) .
In this program you the program generates how many times the
horizontal line of stars to generated and then it makes horizontal
lines comprising of number of stars you stated.

puts " Please enter a number "
s = gets()
while x<s
i=rand(6)
print i
x+=1
while j<i
puts("*")
j+=1
end
end

First problem I see is that j isn't defined... I'm assuming that j is
meant to be some number such that 0 <= j <= 6. Therefore, why not use x
again.

Why not do:

puts "Please Enter a Number:"
s = gets()

x=0
x.upto(s)
i = rand(6) #why the random number geration?
print i
x.upto(i)
puts "*"
end
end

See how that works (I've found that in many cases upto() does more of
what i want to do with a minimum of code and variables than either
whiles or fors).

Lincoln
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFvm7fR8wmeqHdtdcRApW3AKDJqfthO2yURG7QLFeueftgMDiHagCgiFmN
ggHAqF6Yo3u7iQCkBBnMFqI=
=hOKU
-----END PGP SIGNATURE-----
 
L

Lincoln Anderson

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rob said:
While this rings like a homework assignment, I'll answer somewhat
crypticly:

"*" * s }
How many? 19
*******************
*******************
*******************
=> 3
"*" * s }
How many? 7
*******
*******
*******
*******
=> 4

Note that gets() returns a String (see: ri String)
String#to_i converts a string to an integer
"x" * 2 is the same as "xx"
The idiomatic of writing a loop to execute a fixed number of times (say,
3) is 3.times {|c| block }

You never initialize 'j'

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)

D'oh. I missed the simple clues. It does indeed sound like a homework
assignment. Also, your solution looks better.

//i'm still somewhat of a ruby newb

Lincoln Anderson
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFvm/+R8wmeqHdtdcRAseeAJ0UPZSNnGpQWyUu09ysLNZcDqte5gCg2HUu
Hs3a1s53Hn2FmKJtJiJDY8c=
=O6x4
-----END PGP SIGNATURE-----
 
K

Ken Bloom

This is the code I have been trying to execute. But this seems
incorrect. I have trying to make horizontal bar of stars (*) .
In this program you the program generates how many times the
horizontal line of stars to generated and then it makes horizontal
lines comprising of number of stars you stated.

puts " Please enter a number "
s = gets()
while x<s
i=rand(6)
print i
x+=1
while j<i
puts("*")
j+=1
end
end

puts always puts a newline after whatever it prints

all you can ever get from this code is

*
*
*
*
*
*
*

Use print instead.
 
Z

Zunaster

Let me clarify the question.
Objective To generate random stars horizontally.
Input Number of times random stars to be printed e.g. = 2
Display Please enter a number : 2
5 = *****
3 = ***
 
R

Rob Biedenharn

Let me clarify the question.
Objective To generate random stars horizontally.
Input Number of times random stars to be printed e.g. = 2
Display Please enter a number : 2
5 = *****
3 = ***

Let me clarify the answer.

You have been given the information that you need to figure this
out. This is not a tutoring forum for those who can't complete their
assignments.

In fact, you could probably manipulate the program fragments already
provided to get the result you want without increasing your
understanding of the Ruby language.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
Z

Zunaster

You have been given the information that you need to figure this
out. This is not a tutoring forum for those who can't complete their
assignments.

In fact, you could probably manipulate the program fragments already
provided to get the result you want without increasing your
understanding of the Ruby language.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)

I too am not here to tutor myself and get my assignments done. I tried
and did coding but apparently there is something wrong with it. I do
not want ready-made code, but help is what I am asking for. I am
pretty sound in visual studio and transferring my all works from there
in this language. Syntax of ruby is troubling me, otherwise logic is
what pretty much I know. My question demanding clarity so hence I
rephrased my question.

THANKS !
 
C

Chris Shea

I too am not here to tutor myself and get my assignments done. I tried
and did coding but apparently there is something wrong with it. I do
not want ready-made code, but help is what I am asking for. I am
pretty sound in visual studio and transferring my all works from there
in this language. Syntax of ruby is troubling me, otherwise logic is
what pretty much I know. My question demanding clarity so hence I
rephrased my question.

THANKS !

Spend some time with Programming Ruby. The first edition is available
online here: http://www.ruby-doc.org/docs/ProgrammingRuby/

What might be immediately helpful for you is the section on iterators
here: http://www.ruby-doc.org/docs/ProgrammingRuby/html/
tut_expressions.html#UJ

Your next steps should be obvious from there.
 
Z

Zunaster

Thanks everyone. I know I started off programming without much
reading. I have made the required program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,230
Messages
2,571,161
Members
47,796
Latest member
AlphonseNa

Latest Threads

Top