0

How can I use a collection of nested for loops (or any other type) to produce a sequence like this with these variables:

length is how many digts to go to
max is the maximum number
min is the minimum number

Lets say for this case:

length = 2
max = 3
min = 1

it would produce:

11
12
13
21
22
23
31
32
33

This works "ok" for only length = 1, but not really, since I still have annoying 0's at the start

For i = 1 To length
For ii = 0 To i
For iii = 1 To 5
Console.WriteLine(Str(ii) + Str(iii))
Next
Next
Next
9
  • 2
    Have you tried anything? Commented Jul 12, 2013 at 18:41
  • 1
    Dat homework problem :X! Commented Jul 12, 2013 at 18:42
  • Welcome to Stack Overflow! I tried to provide a more descriptive title. Please feel free to edit it if I've failed to capture the essence of your question. Also, please provide some sample code showing what you've tried so far and an explanation as to why it isn't working. Commented Jul 12, 2013 at 18:43
  • Where should the result be placed? Commented Jul 12, 2013 at 18:45
  • 1
    Yep, the console window :) I was just wondering if there was a possible algorithm for it. Also, sorry for syntax issues and formatting and such. Still a noob at this website xD Commented Jul 12, 2013 at 18:48

1 Answer 1

2

As this looks like a homework problem, I am going to attempt to help you think through this problem without actually giving you the answer in code.

Let's think through this problem...

You have the range 1-3. So your first sequence is easy:

1, 2, 3

Now you want to produce a sequence from 11 to 13. What's the change, or difference, between 1 through 3 and 11 through 13? The answer is you've added 10.

The same is true for 21 through 23 - you've added 10 again.

So, what you want to do is iterate from 1 through 3.
Then, iterate from 1 through 3 this time adding 10.
Then, iterate from 1 through 3 this time adding 20.

Thinking about this, you are essentially doing this:

1
2
3
10 + 1
10 + 2
10 + 3
10 + 10 + 1
10 + 10 + 2
10 + 10 + 3
etc

Or, you could also think about it like this:

(0 * 10) + 1
(0 * 10) + 2
(0 * 10) + 3
(1 * 10) + 1
(1 * 10) + 2
(1 * 10) + 3
(2 * 10) + 1
(2 * 10) + 2
(2 * 10) + 3
etc

Can you see a pattern forming?

Sign up to request clarification or add additional context in comments.

6 Comments

I.. can :'D This isnt a homework problem as such. What im trying to do is have min and max as 32 and 126, then transfer these to characters based on their ASCII codes. So it goes: aa ab ac...
@TonyCantone - OK. We do get homework problems on here every now and then, so we try to help students rather than help them cheat. :)
@TonyCantone - Working with Excel, perhaps?
:) Your code works well. Now can/will I use the same method for length = 3? So after 33 it will go 111, 112, 113, 121... When I say "the same method" I mean writing the numbers out and thinking logically about it :P
Ah, wait. Ive just thought of something. Ive been going about this all wrong facepalm. Ive actually just discovered that I just need the possibilities for the given length. So length = 1 would produce 1,2,3. length = 2 would produce 11,12,13,21... Thus the singe characters are not needed
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.