fork(1) download
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. int main( ) {
  8.  
  9. unsigned int r, q;
  10.  
  11. std::cout << "Insert r and q for Ham(r,q):\t";
  12. std::cin >> r;
  13. std::cin >> q;
  14.  
  15. unsigned int s = (unsigned int)( pow(q,r) -1 )/(q-1);
  16.  
  17. unsigned int **H = new unsigned int*[r];
  18.  
  19. unsigned int* F_q = new unsigned int[ q ];
  20.  
  21. for( int i=0; i < q; i++ )
  22. F_q[i] = i;
  23.  
  24. for(int i = 0; i < r; ++i) {
  25. H[i] = new unsigned int[s];
  26. memset( H[i], 0, s*sizeof( unsigned int ) );
  27. }
  28.  
  29. int fill_with_ones = 0, current_ones_row = 0, cumulative_row = 0, tick = 0, current = 0;
  30. bool increase;
  31.  
  32. for( int row = 0; row < r; row++ ) {
  33.  
  34. increase = true;
  35.  
  36. current_ones_row = (unsigned int)pow( q, r-1-fill_with_ones );
  37. cumulative_row += current_ones_row;
  38.  
  39. current = 0;
  40.  
  41. for( int col = 0; col < s; col++ ) {
  42.  
  43. tick = (int)pow(q, r-1-row);
  44.  
  45. if( col >= cumulative_row - current_ones_row && col < cumulative_row ) {
  46. H[ row ][ col ] = 1;
  47. current_ones_row--;
  48. } else { // Entro qui dentro se sono fuori dall'intervallo di uni
  49. if( increase ) {
  50. fill_with_ones++;
  51. increase = false;
  52. }
  53.  
  54. if( col >= cumulative_row )
  55. break;
  56. else
  57. if( col < cumulative_row - current_ones_row ) { // Entro qui dentro quando sono sotto gli uni, regione da riempire
  58. H[ row ][ col ] = F_q[ current ];
  59.  
  60. if( (col+1) % tick == 0 ) {
  61. if( (tick == 1 && col == 0) || col != 0 )
  62. current = (current+1) % q;
  63. }
  64.  
  65. }
  66. }
  67.  
  68.  
  69. }
  70.  
  71. }
  72.  
  73. std::cout << "\nParity check matrix:\n";
  74. for( int i=0; i < r; i++ ) {
  75. for( int j=0; j < s; j++ )
  76. std::cout << H[i][j] << " ";
  77.  
  78. std::cout << std::endl;
  79. }
  80.  
  81. return 0;
  82. }
Success #stdin #stdout 0s 5704KB
stdin
4 3
stdout
Insert r and q for Ham(r,q):	
Parity check matrix:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 0 0 0 0 
0 0 0 1 1 1 2 2 2 0 0 0 1 1 1 2 2 2 0 0 0 1 1 1 2 2 2 0 0 0 1 1 1 2 2 2 1 1 1 0 
0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 1