Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Algorithm to create possibility of a number ?? it's possible??
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Manqueba




PostPosted: Tue May 16, 2017 8:10 pm   Post subject: Algorithm to create possibility of a number ?? it's possible??

I need help building an algorithm.
This algorithm needs to fill a "database", which
Creates all the possibilities of a set of numbers.
For example: 4
4! -> 4 * 3 * 2 * 1 = 24

1234 2134 3124 4123
1243 2143 3142 4132
1324 2314 3214 4213
1342 2341 3241 4231
1423 2413 3412 4312
1432 2431 3421 4321

Is it possible to create this algorithm and work for the other numbers ??
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed May 17, 2017 6:38 pm   Post subject: RE:Algorithm to create possibility of a number ?? it\'s possible??

Yes, it's possible. What have you tried? Where are you getting stuck?
Manqueba




PostPosted: Wed May 17, 2017 7:22 pm   Post subject: RE:Algorithm to create possibility of a number ?? it\'s possible??

I tried to use random numbers, it did not work. I want to populate a matrix with the figures separately. In the example I put in the question I can note that each number repeats in proportion to the value of the factorial number divided by the size of the sequence.
Insectoid




PostPosted: Sun May 21, 2017 1:42 pm   Post subject: RE:Algorithm to create possibility of a number ?? it\'s possible??

You don't want random numbers, you want a specific pattern of numbers. A specific pattern is a far cry from a random pattern, so why on earth would you think random numbers would work?

How do you plan on storing your matrix of numbers? Well, it looks like for an input of N, you have N! groups of numbers. Each group of numbers is of size N. It makes sense then to store it in a 2-dimensional array of size [N!][N]. In order to populate this array, you need to iterate over every cell. The easiest way to iterate over every cell in a multi-directional array is with nested loops. The outer loop iterates from 0 to N!-1 while the inner loop iterates from 0 to N-1.

Once you have set this up, the actual act of populating the array will hopefully be obvious as that is what this assignment is trying to teach you.
Manqueba




PostPosted: Sun May 21, 2017 6:23 pm   Post subject: Re: Algorithm to create possibility of a number ?? it's possible??

I have tried to traverse the matrix in this way, but I have not been able to understand how to fill it in such a way that neither possibility is equal to another.
Insectoid




PostPosted: Sun May 21, 2017 6:24 pm   Post subject: RE:Algorithm to create possibility of a number ?? it\'s possible??

What happens if you simply output the for loop variables?

Edit: Oops, I'm giving bad advice. It's a wee bit more complicated than I let on. My method will work, with some extra effort. The are more efficient though more complicated solutions available. What you're trying to do is generate all permutations of a set. Googling that will give you a number of different algorithms to achieve that.
Manqueba




PostPosted: Sun May 21, 2017 6:57 pm   Post subject: Re: Algorithm to create possibility of a number ?? it's possible??

Thanks for the advice. I think I'm almost done.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: