[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Horizontal Stars

Zunaster

1/29/2007 9:35:00 PM

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

9 Answers

Rob Biedenharn

1/29/2007 9:56:00 PM

0


On Jan 29, 2007, at 4:35 PM, Zunaster wrote:

> 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:

>> print "How many? ";s=gets.chomp.to_i; rand(6).times {|counter|
puts "*" * s }
How many? 19
*******************
*******************
*******************
=> 3
>> print "How many? ";s=gets.chomp.to_i; rand(6).times {|counter|
puts "*" * 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://agileconsult...
Rob@AgileConsultingLLC.com



Lincoln Anderson

1/29/2007 10:02:00 PM

0

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

Zunaster wrote:
> 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....

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

Lincoln Anderson

1/29/2007 10:07:00 PM

0

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

Rob Biedenharn wrote:
>
> On Jan 29, 2007, at 4:35 PM, Zunaster wrote:
>
>> 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:
>
>>> print "How many? ";s=gets.chomp.to_i; rand(6).times {|counter| puts
> "*" * s }
> How many? 19
> *******************
> *******************
> *******************
> => 3
>>> print "How many? ";s=gets.chomp.to_i; rand(6).times {|counter| puts
> "*" * 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://agileconsult...
> Rob@AgileConsultingLLC.com
>
>
>
>

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....

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

Ken Bloom

1/30/2007 1:38:00 AM

0

On Mon, 29 Jan 2007 13:34:32 -0800, Zunaster wrote:

> 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.

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Zunaster

1/30/2007 12:13:00 PM

0


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 = ***


Rob Biedenharn

1/30/2007 1:54:00 PM

0

On Jan 30, 2007, at 7:13 AM, Zunaster wrote:
> 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://agileconsult...
Rob@AgileConsultingLLC.com



Zunaster

1/30/2007 4:59:00 PM

0



On Jan 30, 5:54 am, Rob Biedenharn <R...@AgileConsultingLLC.com>
wrote:
> On Jan 30, 2007, at 7:13 AM, Zunaster wrote:
>
> > 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://agileconsult...
> R...@AgileConsultingLLC.com

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 !


Chris Shea

1/30/2007 6:02:00 PM

0

On Jan 30, 9:59 am, "Zunaster" <zunaira.so...@gmail.com> wrote:
> On Jan 30, 5:54 am, Rob Biedenharn <R...@AgileConsultingLLC.com>
> wrote:
>
>
>
> > On Jan 30, 2007, at 7:13 AM, Zunaster wrote:
>
> > > 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://agileconsult...
> > R...@AgileConsultingLLC.com
>
> 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/Progra...

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

Your next steps should be obvious from there.

Zunaster

1/30/2007 6:53:00 PM

0


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