Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // create binary vector, that matches givven equation
- int binary_set[8] = {0, 0, 0, 0, 0, 1, 1, 1};
- // left transposition function
- int transposition();
- int shift(int);
- void print_iter();
- int main() {
- int tpos;
- print_iter();
- while( (tpos = transposition()) != -1 ) {
- shift(tpos);
- print_iter();
- }
- }
- int transposition() {
- int i;
- for (i =1; i < 8; i++ ) {
- if( binary_set[i] == 1 && binary_set[i - 1] == 0 ) {
- printf("Tr_position %d\n", i);
- binary_set[i] = 0;
- binary_set[i - 1] = 1;
- return i - 1; // 1 position returned
- }
- }
- return -1;
- }
- int shift(int tpos ) {
- // 1 number appeares in elder index
- if(tpos < 1)
- return -1;
- // no 1 numbers in elder index
- if(binary_set[0] != 1)
- return -1;
- int count_left_zeros = 0;
- int i;
- for (i = 1; i < tpos; i++ ) {
- if(binary_set[i] == 0)
- count_left_zeros++; // 0 amount before swaped pare 01 -> 10
- }
- if (count_left_zeros > 0) {
- for(i = 0; i < count_left_zeros; i++)
- binary_set[i] = 0; // set 0 numbers
- for( i; i < tpos; i++)
- binary_set[i] = 1; // set 1 numbers
- }
- }
- void print_iter() {
- static int i = 0;
- printf("Iteration index %d: [", i++);
- int y;
- for(y = 0; y < 7; y++)
- printf("%d, ", binary_set[y]);
- printf("%d]\n", binary_set[y]);
- }
Advertisement
Add Comment
Please, Sign In to add comment