user3171597
user3171597

Reputation: 457

How to do permutation when mapping an array of integers to array of strings?

How would I write a permutation method in C programming with an array of string characters while each character is being mapped to an integer? So for example, I have a permutation of 10 integers:

1, 0, 2, 3, 5, 4, 6, 9, 8, 7 - not necessarily in order

and a text full of characters, less than 400 characters. I have both of them stored into 2 separate arrays, but how would I map each integer onto each character? After mapping them, how would I go about writing a method of permutation?

Neither arrays are the same size. The permutation array has 10 values, the char array has less than 400 chars, but I just initialized it as: char text[400]; the size of each arrays do not move.

Better explanation:

I was thinking, if I have 10 integers (0 - 9) and the following permutation - where each permutation cannot have the same number:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9 - original

1, 0, 3, 6, 7, 4, 5, 9, 2, 8 - permutation

Apple, bee - text of 10 characters, including the space and comma

so by applying the permutation above, I would get something like:

'pAl be,epe'

And if you use the inverse of that permutation, you would get 'Apple, bee' again.

The original index[0] is where the 'A' in Apple is supposed to be - and with the permutation, it is switched with the 'p' because of the 1 index... and so on.

So how would I go about implementing that? In other words, how would I map the permutation to each char and have it switch positions? I haven't learned about Structures yet.

Upvotes: 0

Views: 380

Answers (1)

Orelsanpls
Orelsanpls

Reputation: 23515

First case:

Simply one is to make a implicit link between your array.

Array[int]    -->      Array[string]           
   1          -->           "toto"              Array[0]
   0          -->           "titi"              Array[1]
   2          -->           "tutu"              Array[2]
  ...

And then if you want to permut two element you can move strings pointers

Second case:

You want a real link between your array, so i recommand to make a structure :

struct example
{
   int ArrayInt[10];
   char *ArrayPointer[10];
};

Where ArrayPointer will contain the address of the string situated in the array of string.

Example :

           STRUCTURE

Array[int]   Array[char *]                    ArrayString

0            &ArrayString[1]                  "toto"

1            &ArrayString[0]                  "titi"

2            &ArrayString[2]                  "tutu"

Number 0 is mapped with "titi"

Number 1 with "toto"

Number 2 with "tutu"

To make a permutation, simply change the pointer in the structure

Is it ok?

Upvotes: 1

Related Questions