Author |
Message |
jinjin
|
Posted: Sun Mar 16, 2008 5:40 pm Post subject: nested for loops |
|
|
Sorry for posting a question again but I keep getting stuck on these dreaded for loops assignments. Please don't regard my lots of question posting as spamming. Thanks.
Anyways, the program I am writing is supposed to ask the user how many times they wish to count from 1 to 20 and then output a 1 to 20 loop repeated by the number of times inputted by the user.
Here's what I have so far:
code: |
var times : int
put "How many times do you want to count from 1 to 20?"
get times
for c : 1 .. 20
for b : 1 .. times
put c
end for
end for
|
The program works perfectly when the number 1 is inputted but when any other number is entered, it counts from 1 to 20 but repeats every number by the input for 'times.' (i.e. for an input of 2, it will output 1 1 2 2 3 3 4 4 ... 20 20). I'm not sure how to configure b or c to fix this. I have tried many different combinations but all have either errors or totally messed up outputs. Any help is appreciated. Thanks  |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Sean

|
Posted: Sun Mar 16, 2008 5:51 pm Post subject: Re: nested for loops |
|
|
Check the order of the loops, it's doing it twentie times, then putting it depending on the number you input.
Re-organize. |
|
|
|
|
 |
A.J

|
Posted: Sun Mar 16, 2008 6:28 pm Post subject: Re: nested for loops |
|
|
what vilament is trying to say is that if the person inputs 2, you output each number from 1-20 twice, and so on...
you got the ordering of the for loops mixed up, since you want to output the numbers from 1-20 'times' number of times.
so you make a simple for loop for the counting from 1-20:
Turing: |
for i:1..20
put i
end for
|
but you should have a bigger for loop around this one to tell the computer that you want to out put the numbers that many times:
Turing: |
for j:1..times
for i:1..20
put i
end for
end for
|
don't get used to me coming to the rescue  |
|
|
|
|
 |
Sean

|
Posted: Sun Mar 16, 2008 6:34 pm Post subject: Re: nested for loops |
|
|
I was hoping for them to do A.J.
They could of solved it. |
|
|
|
|
 |
A.J

|
Posted: Sun Mar 16, 2008 6:40 pm Post subject: Re: nested for loops |
|
|
well...
you may never know (i wont help people like this next time ) |
|
|
|
|
 |
jinjin
|
Posted: Sun Mar 16, 2008 6:43 pm Post subject: Re: nested for loops |
|
|
@ Vilament : I tried reordering the loops and it didn't work for some reason. I was still looking at it so that's why I did not reply for so long.
@ A.J: Thanks for coming to the rescue again, you are a lifesaver
@ Vilament : I appreciate your advice equally. I too try working it out myself before looking to the forum.
@ A.J: Sorry for the rescue thing again, I'll try keeping this to a minimum
So, thanks both Vilament and A.J!!! +12 bits to both. This CompSci forum is a great community. |
|
|
|
|
 |
A.J

|
Posted: Sun Mar 16, 2008 7:03 pm Post subject: Re: nested for loops |
|
|
your welcome
feel free to ask for help again, we're not stopping you  |
|
|
|
|
 |
|