Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. /* Задание:
  2.  
  3. Через канал с шумами передается последовательность битов.
  4. Если бит передан корректно, то следующий бит передан корректно с
  5. вероятностью 0.8.
  6. Если бит передан некорректно, то следующий бит передан некорректно с
  7. вероятностью 0.7.
  8. Определить процент некорректных битов. Т.е. до конца занятия студент
  9. должен сообщить свою фамилию и 1 число. Решать можно как угодно,
  10. аналитически или используя имитационное моделирование.
  11.  
  12. Ответ: ~40% */
  13.  
  14. //Mayatsky Alexander, 2010
  15.  
  16. #include <iostream>
  17. #include <ctime>
  18. #define LOOP_ITERATIONS 1000000
  19.  
  20. /* Nextbit determination function */
  21. bool nextbit( bool previousbit ){
  22.    
  23.     if ( previousbit ){
  24.         if ( std::rand() % 101 <= 80 ){
  25.             return true;
  26.         }
  27.         else return false;
  28.     }
  29.        
  30.    
  31.     if ( ! previousbit ){
  32.         if ( std::rand() % 101 <= 70 ){
  33.             return false;
  34.         }
  35.         else return true;
  36.     }
  37.  
  38. return false; //This return placed here to supress warnings only.
  39.  
  40. }
  41.  
  42. int main( int argc, char *argv[] ){
  43.  
  44.     /* Random seed initialising, using unixtime */
  45.     time_t ltime;
  46.     time ( &ltime );
  47.     srand( (unsigned int)ltime );
  48.  
  49.     /* Main Loop */
  50.     int correctbits = 0;
  51.     int failbits = 0;
  52.     bool currentbit = false;
  53.     for ( int i = 0; i < LOOP_ITERATIONS; i++ ){
  54.         currentbit = nextbit(currentbit);
  55.         if ( currentbit ){
  56.             correctbits++;
  57.         }
  58.         else {
  59.             failbits++;
  60.         }
  61.     }
  62.    
  63.     /* Results output */
  64.     std::cout << (float)correctbits / LOOP_ITERATIONS * 100 << '%' << " of correct bits\n";
  65.     std::cout << (float)failbits / LOOP_ITERATIONS * 100 << '%' << " of fail bits\n";
  66.  
  67.     std::getchar();
  68.    
  69. }
  70.  
  71. /* Typical output:
  72.  
  73. 60.0482% of correct bits
  74. 39.9518% of fail bits
  75.  
  76. 59.8607% of correct bits
  77. 40.1393% of fail bits
  78.  
  79. 59.9462% of correct bits
  80. 40.0538% of fail bits
  81.  
  82. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement