Computer Science Canada

how do i solve this loop

Author:  hpdudette [ Thu May 22, 2003 6:57 pm ]
Post subject:  how do i solve this loop

this is the problem:
write a program containing an infinite loop which out puts a series of integers starting at 5 and going up by 5

Author:  kythoon [ Thu May 22, 2003 7:01 pm ]
Post subject: 

code:

var number : int := 5


put number
loop
    number := number + 5
    put number
end loop


i think this is what u want

Author:  Catalyst [ Thu May 22, 2003 7:02 pm ]
Post subject: 

not gonna solve for u but heres a hint, use a loop
its REALLY simple
5 lines max


note: kythoon posted while i was posting

Author:  hpdudette [ Thu May 22, 2003 7:02 pm ]
Post subject: 

Laughing Laughing Laughing Very Happy Very Happy Very Happy thank you soooooo much

Author:  netninja [ Mon Feb 16, 2004 7:55 am ]
Post subject: 

Would it not be easier to make it count 'by '5's instead of +5??? Its not even sarcasm, i'd like to know. If the numbers start getting big, will the program count faster 'by' numbers, or + the numbers??

Author:  Tony [ Mon Feb 16, 2004 8:57 am ]
Post subject: 

Confused what do you mean?

and the answer is no Laughing

Author:  Cervantes [ Mon Feb 16, 2004 3:45 pm ]
Post subject: 

Well I think you learned a bit from my tutorial Smile unfortunately, I think you''re getting for loops and loops confused.

The 'by 5' you are getting from a for loop when you do something like this

code:

for counter : 1 .. 100 by 5
%blah blah blah
end for


That would work, except you can't use for loops in this problem because it asks to repeat an infinate number of times.

Loops = go on forever until the exit when statement is true
for loops = repeat a defined number of times. (can't set it to be 1 .. infinity)

Author:  netninja [ Tue Feb 17, 2004 6:57 pm ]
Post subject: 

Yes, that was my question i think, if it was possible to do an infinite loop with the 'by', but i soon realized i had to do the num:=num+5 method. I already understand that. But thanks anyway.

Author:  netninja [ Wed Feb 18, 2004 4:08 pm ]
Post subject: 

How do you make a loop that generates random integers. My code isnt good at all. I tried doing a for loop, like 1..5, (so i generate 5 random numbers) and randint, but my code is all wrong, 0 % accurate. Can someone help me with that?

Author:  Cervantes [ Wed Feb 18, 2004 4:20 pm ]
Post subject: 

code:

var randnum : int
loop
   randnum := Rand.Int (lownum, highnum)
 %could also do it like this:
  %randint (randnum, lownum, highnum)
   put randnum
end loop


or if you want to use a for loop

code:

var randnum : int
for i : low .. high
  randnum := Rand.Int (lownum, highnum)
  put randnum
end for

Author:  netninja [ Wed Feb 18, 2004 8:44 pm ]
Post subject: 

But i would like to make it give 5 random integers. I already know how to put 1 Laughing AHHHHhhhhHHHH, nevermind, i just noticed something. Thanks alot Cervantes. Sorry for my stupidity. heh, so that code wouldnt work with randint? Only Rand.Int. And i just realized why it fits into this particular code.

Thanks again

Author:  TheZsterBunny [ Thu Feb 19, 2004 8:21 am ]
Post subject:  This may be too late but,

This is something you could use


code:
var low := 1
var high := 5
var rhandom : array low..high of int
for randemnity: low..high
rhandom(randemnity) := Rand.Int(1,5)
end for


That would work quite well, but arrays take up a great deal of space.

-z0rQ


: