BorrowTheProgrammer

lab1_MKV

Oct 4th, 2021
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // create binary vector, that matches givven equation
  4. int binary_set[8] = {0, 0, 0, 0, 0, 1, 1, 1};
  5.  
  6. // left transposition function
  7. int transposition();
  8.  
  9. int shift(int);
  10.  
  11. void print_iter();
  12.  
  13. int main()  {
  14.  
  15.     int tpos;
  16.     print_iter();
  17.    
  18.     while( (tpos = transposition()) != -1 )  {
  19.  
  20.         shift(tpos);
  21.         print_iter();
  22.     }
  23.  
  24. }
  25.  
  26. int transposition()  {
  27.  
  28.     int i;
  29.  
  30.     for (i =1; i < 8; i++ )  {
  31.  
  32.         if( binary_set[i] == 1 && binary_set[i - 1] == 0 )  {
  33.  
  34.             printf("Tr_position %d\n", i);
  35.             binary_set[i] = 0;
  36.             binary_set[i - 1] = 1;
  37.            
  38.             return i - 1; // 1 position returned
  39.         }
  40.     }
  41.     return -1;
  42. }
  43.  
  44. int shift(int tpos )  {
  45.    
  46.     // 1 number appeares in elder index
  47.     if(tpos < 1)
  48.         return -1;
  49.        
  50.     // no 1 numbers in elder index
  51.     if(binary_set[0] != 1)
  52.         return -1;
  53.  
  54.     int count_left_zeros = 0;
  55.    
  56.     int i;
  57.  
  58.     for (i = 1; i < tpos; i++ )  {
  59.  
  60.         if(binary_set[i] == 0)
  61.             count_left_zeros++; // 0 amount before swaped pare 01 -> 10
  62.     }
  63.  
  64.     if (count_left_zeros > 0)  {
  65.  
  66.         for(i = 0; i < count_left_zeros; i++)
  67.             binary_set[i] = 0; // set 0 numbers
  68.  
  69.         for( i; i < tpos; i++)
  70.             binary_set[i] = 1; // set 1 numbers
  71.     }
  72. }
  73.  
  74. void print_iter()  {
  75.  
  76.     static int i = 0;
  77.    
  78.     printf("Iteration index %d: [", i++);
  79.    
  80.     int y;
  81.    
  82.     for(y = 0; y < 7; y++)
  83.         printf("%d, ", binary_set[y]);
  84.  
  85.     printf("%d]\n", binary_set[y]);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment