Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 3.58 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C   Keeping Track of the Longest Streak
  2. int main(){
  3.  
  4.     int number_of_flips;
  5.     int coin_flip;
  6.     int previous_flip = 2;
  7.     int head_count = 0;
  8.     int tail_count = 0;
  9.     int highest_head = 0;
  10.     int highest_tail = 0;
  11.  
  12.     srand(time(NULL));
  13.  
  14.     cout << "Enter the number of coin flips:" << endl;
  15.     cin >> number_of_flips;
  16.     system("cls");
  17.  
  18.     for(int i = 0; i < number_of_flips; i++){
  19.  
  20.  
  21.         coin_flip = rand() % 2;
  22.  
  23.         if(coin_flip == 0){
  24.             cout << "Heads" << endl;
  25.             if(coin_flip == previous_flip){
  26.                   head_count = head_count + 1;      
  27.              }
  28.              else{
  29.                  if(head_count > highest_head){
  30.                      highest_head = head_count;                  
  31.                  }
  32.  
  33.                  head_count = 0;      
  34.              }
  35.         }
  36.  
  37.         if(coin_flip == 1){
  38.             cout << "Tails" << endl;
  39.             if(coin_flip == previous_flip){
  40.                   tail_count = tail_count + 1;        
  41.             }
  42.             else{
  43.                  if(tail_count > highest_tail){
  44.                       highest_tail = tail_count;              
  45.                  }
  46.  
  47.                  tail_count = 0;    
  48.             }
  49.         }
  50.  
  51.  
  52.         previous_flip = coin_flip;
  53.     }
  54.  
  55.     cout << "The longest run of heads is " << highest_head << endl;
  56.     cout << "The longest run of tails is " << highest_tail << endl;
  57.  
  58.     system("pause");
  59.     return 0;
  60. }
  61.        
  62. Tails
  63. Tails
  64. Tails
  65. Heads
  66. Heads
  67. Tails
  68. Tails
  69. Tails
  70. Tails
  71. Heads
  72. The longest run of heads is 1
  73. The longest run of tails is 2
  74.        
  75. int main(){
  76.  
  77.     int number_of_flips;
  78.     int coin_flip;
  79.     int previous_flip = 2;
  80.     int head_count = 0;
  81.     int tail_count = 0;
  82.     int highest_head = 0;
  83.     int highest_tail = 0;
  84.  
  85.     srand(time(NULL));
  86.  
  87.     cout << "Enter the number of coin flips:" << endl;
  88.     cin >> number_of_flips;
  89.     system("cls");
  90.  
  91.     for(int i = 0; i < number_of_flips; i++){
  92.  
  93.  
  94.         coin_flip = rand() % 2;
  95.  
  96.         if(coin_flip == 0){
  97.             cout << "Heads" << endl;
  98.             if(coin_flip == previous_flip){
  99.                   head_count = head_count + 1;      
  100.              }
  101.              else{
  102.                  if(head_count > highest_head){
  103.                      highest_head = head_count;                  
  104.                  }
  105.  
  106.                  head_count = 1;      
  107.              }
  108.         }
  109.  
  110.         if(coin_flip == 1){
  111.             cout << "Tails" << endl;
  112.             if(coin_flip == previous_flip){
  113.                   tail_count = tail_count + 1;        
  114.             }
  115.             else{
  116.                  if(tail_count > highest_tail){
  117.                       highest_tail = tail_count;              
  118.                  }
  119.  
  120.                  tail_count = 1;    
  121.             }
  122.         }
  123.         previous_flip = coin_flip;
  124.     }
  125.     if(head_count > highest_head){
  126.            highest_head = head_count;              
  127.     }
  128.     if(tail_count > highest_tail){
  129.            highest_tail = tail_count;              
  130.     }
  131.     cout << "The longest run of heads is " << highest_head << endl;
  132.     cout << "The longest run of tails is " << highest_tail << endl;
  133.  
  134.     system("pause");
  135.     return 0;
  136. }
  137.        
  138. Tails   // Does not count this one because flip != last_flip
  139. Tails   // tail_count is 1
  140. Tails   // tail_count is 2
  141. Heads   // Does not count first flip on flip switch, reset head_count to 0
  142. Heads   // head_count is 1
  143. Tails   // Does not count first flip, set max_tail to 2, reset tail_count to 0
  144. Tails   // tail_count is 1
  145. Tails   // tail_count is 2
  146. Tails   // tail_count is 3 but will never be set unless we flip head, then tail.
  147. Heads   // Does not count first switch, set max_head to 1, reset head_count to 0