Click here to Skip to main content
15,886,056 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to make a game in C for which I need to generate a set of all possible "sequence of numbers" for a given code length of the sequence and given range of digits in the sequence of numbers.

for eg : if the code length is 4 and the range is from 0 to 8

then total no. of possiblities are 9^4

I want to create a set containing all these possiblities. Any suggestions on how I should go about it?

Please bear in mind I am a beginer and started learning C just a few weeks back.

Thank You! :)
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jan-13 16:39pm    
Did you ever head of the principle "garbage in — garbage out"? You describe what do you have on input, and we can probably tell you how to get the output.

But you also should tell us what's the problem, why you cannot do it by yourself. To list possibilities, at least you should tell us how just one "possibility" object looks like.

—SA

Your problem can be simplified to the topic of number systems. In your sample you talk about a 4 digit number in the number 9 system. Why? Think of this one "the code length is 4 and the range is from 0 to 9" - and you have all 10000 numbers from 0000 to 9999. You got it?
So you simply have to count from 0000 to 8888 in your case (or to any other value).
The algorithm is simple:
 0) let N be the code length, and D the highest value.
 1) declare an array of int with N elements
 2) set all values to 0
 3) add 1 to the last element
 4) loop from the last to the first element:
 5)   if the current element is higher than D, reset to 0, 
 6)      if there is a previous element, increment the previous element
 7)         else you passed the last element, finished
 8)      else exit loop
 9) PROCESS array
10) go to step 3

You can add an extra condition to stop at the last element and not to pass it, only if you need it. In step 9 the array will contain the "number" you need.
You can extend this to any set of "digits", if you use the array value as index.
 
Share this answer
 
v4
Comments
Member 9720070 18-Jan-13 18:11pm    
I don't understand : 6) if there is a previous element, increment the previous element.
9)PROCESS array

Thanks for helping me out! :)
Zoltán Zörgő 19-Jan-13 5:40am    
6) Let's suppose your example and your current value is 0088. By adding 1 to the end you would get 00889, but you can't have 9, so you reset it to 0 and increment the previous one 0098, but you can't have 9 at that position either, so you repeat the above step, and you get 0100. But if you have 8888, than you would get 8889, than 8890, than 8900, than 9000, but there is no element before 9 to increment, so you end with 0000.
9) this is the part of the code where you do with your current value what you originally wanted, like printing it.
If I understand correctly, what you want to do is something like:

Given A,B,C, generate all possible strings of length 2 => AA, AB, AC, BA, BB, BC, CA, CB, CC

In that case, you'll want to find code for generating permutations in C. Google should be of great help there, I don't know of any good C examples myself, I've only done stuff involving permutations in Python which has support for them in its standard library.
 
Share this answer
 
Comments
Member 9720070 18-Jan-13 18:04pm    
Okay! thanks!
Zoltán Zörgő 19-Jan-13 5:43am    
Wrong. There is no such thing as permutation of 3 element on 2 positions. You can either generate permutation of 2 or 3 elements, but that means all possible orderings, nothing more. The number of permutations of n elements is n!.
lewax00 19-Jan-13 14:13pm    
This disagrees with you (second paragraph in the linked section): http://en.wikipedia.org/wiki/Permutation#In_combinatorics technically it's a partial permutation, but it is still referred to as a permutation.
Zoltán Zörgő 19-Jan-13 15:42pm    
Well, you have to argue with the mathematicians. I have to agree with them, not with the Wikipedia authors.
I would like to build upon Zoltan solution. So you understand that you want to enumerate all numbers from 0000 to 8888 in a number system with base 9.

There is a pretty easy way to do that. Just use an int variable an increase it step wise. The value in an int is stored in the base 2 number system and for us humans we tend to convert it to decimal (base 10) whenever we display or print its value. But we can equally well output it in any other base, for example by
C++
int i;
char buffer [20];
itoa (i, buffer, 9); // convert i into a string in buffer with base 9

So all you have to do is count from 0 to some value and convert that value in way shown above to the base 9 system. Now, what is the value you need to count to? So what is the correspondence of 10000(base9) in decimal? That is the value at which we have to stop counting. It is
C++
limit = 8 * 8 * 8 * 8;

So here is the simple version:
C++
int i;
int limit = 8 * 8 * 8 * 8;
char buffer[20];

for (i = 0; i < limit; ++)
{
    itoa (i, buffer, 9);
    /* do something with buffer, put it in array for example */
}


The only downside is that itoa does not produce leading zeros. So you will get "0" instead of "0000" for the first value. If that bothers you, you might consider to do the conversion into base 9 yourself, for example:
C++
char d;

for (j = 0; j < 4; ++j)
{
    d = (i % 9) + '0';
    buffer [3 - j] = d;
    i /= 8;
}
buffer[4] = '\0';


I hope that gave you some ideas.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900