Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Permutation processing is both interesting and important.
This page looks at some aspects of permutation processing.
Note: This is a more advanced program that will be presented. The final program is something that could be used in an introductory programming course but is not something that could be developed from scratch by students in an introductory programming course.
The module developed will not use any global variables.
This makes it a little less machine efficient, but is satisfactory for the present purposes since it provides a cleaner design for understanding purposes.
2. Applications
Permutations are used in many applications.
Secret codes and security
Solving certain types of constraint problems
Games, puzzles, etc.
3. Definition
A mathematical and recursive definition of a permutation of n objects is as follows.
The permutation of 1 object is the object itself.
The permutation of n objects (n > 1) is obtained by taking each object of the n objects and permuting it with every permutation of the remaining n-1 objects.
4. Development
We will use a swap procedure defined as follows. For more information, see Procedure to swap values .
5. Data list
A data list contains the data to be processed, as in the following.
6. Array element pointers
It is often easier and better to work with pointers to array elements rather than the array elements themselves. An array of integer elements can be used such that each integer element is and index to another array.
7. Array indirection
Here is an example of indirect access (via array pointers).
Since the data is accessed indirectly (via the array pointer) this process is called indirection.
Here is the C code.
8. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
To do this, start with the following data structures.
DATAMAX is the maximum number of elements to permute.
dataList1 is the list of array elements to be permuted.
dataLen1 is the number of elements in the list, starting at 1.
permuteList1 is the list of array element pointers to the array containing the values.
permuteCount1 is the count of the current permutation
Arrays dataList1 and permuteList1 are parallel arrays.
9. Get the input
The input is read, line by line, using gets until the end of file is reached.
10. Show the permutation
The following procedure showDo1 is used to show the permutation found.
The global value permuteCount1 is used to keep track of the number of permutations found/processed.
11. Action
The procedure actionDo1 is the procedure called for each new permutation. Any selection or criteria used in constraint programming can be placed here (in the brute force approach).
For this example, there is no selection criteria and every permutation is displayed using show1.
12. Permute procedure
Here is the recursive permute procedure permute1.
Note the passing of a function (pointer) to permute1.
To perform permutations, one calls procedure permute0 defined as follows.
Note the passing of a function (pointer) to permute0. That action passed as a function will be the "callback" action.
13. Interface abstraction
The interface is abstracted as follows.
14. Complete program
Here is the C code.
15. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
16. Module abstraction
Programming languages have various ways of abstracting and encapsulating code to be used between different programs. Some terms for these are the following - which are not all the same but have the same very general meaning.
library
module
package (e.g., Python)
namespace (e.g., C# and .NET languages)
unit (e.g., Delphi)
class (e.g., Java)
17. rsPermute module
In C, the interface becomes a .h file while the implementation becomes a .c file.
For the abstraction of the permutation code, the name used will be rsPermute.
Here is a code module in C [module rsPermute in rsPermute.c].
18. rsPermute header
Here is a code interface in C [header rsPermute in rsPermute.h].
19. Header guards
In C (and other code) that use include, a common way to avoid including a header file more than once is the following.
Including a header file more than once can cause errors.
1. Define a symbol for the module, such as RSPERMUTE_H (for header file rsPermute.h)
2. Put the following at the start of the header (interface) file.
#ifndef RSPERMUTE_H
#define RSPERMUTE_H
3. Put the following at the end of the header (interface) file.
#endif
The first time the header file is included, symbol RSPERMUTE_H is not defined. So the preprocessor defines that symbol and then includes the header body.
If the header file is included again (nested includes), then the symbol RSPERMUTE_H is already defined so that header would not be included again.
20. Program using rsPermute
Here then is the above program using rsPermute.
Here is the C code.
21. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
22. Make
Note that when using modules, the gcc command to compile the program needs to include any used modules. Here is an example.
gcc myprog1.c rsPermute.c -o myprog1.exe
When a program needs a lot of modules, and those modules need other modules, etc., this process can get very complex.
Much of this complexity can be hidden in a make command.