Complex permutation of elements of an array

I have written two rotation functions that operate on a character array of length equal to a perfect square.
The first rotate n times each odd line to the right and n times each pair line to the left. The second rotates n times each odd column down and n times each pair column up, where n is the numer of current line or column.

Example:
abcde
fghij
klmno
pqrst
uvwxy

Becomes:
eabcd
hijfg
mnokl
tpqrs
uvwxy

And finally:
unoxd
epqcg
hvwfl
mabks
tijry

I would like to reduce everything to a single function, or rather, to a single loop that moves each character in its final position without performing all the steps.
You can do this? Any suggestions?
How many loop you use now? Why don't you show your code to get the whole picture and not talk in the air?
I've tried something similar:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char txt[] = "abcdefghijklmnopqrstuvwxy";
int r = sqrt(strlen(txt));
char tmp[r][r];
int y, z, x = 0;

for(y = 0; y < r; y++)
{
    for(z = 0; z < r - 1; z++)
    {
	if(((z + (y + 1)) % 2) == 1)
	{
          tmp[(r - (z + (y + 1)) - 1)][(z + (y + 1)) % r] = txt[x++];
	}
	else
	{
		tmp[(z + (y + 1) + 1) % r][(z + (y + 1)) % r] = txt[x++];
	}
    }
	tmp[(y + 1) % r][(z + (y + 1)) % r] = txt[x++];
}


It isn't correct.
Last edited on
Topic archived. No new replies allowed.