Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Keeping Track of the Longest Streak
    primarykey
    data
    text
    <p>For my computer programming class, I decided to make a program that generates a random coin flip and keeps track of the longest consecutive streak for both heads and tails. I have searched the internet and found no answer. The count is not displaying right. Even just a hint would be great! Thanks, Justin</p> <pre><code>int main(){ int number_of_flips; int coin_flip; int previous_flip = 2; int head_count = 0; int tail_count = 0; int highest_head = 0; int highest_tail = 0; srand(time(NULL)); cout &lt;&lt; "Enter the number of coin flips:" &lt;&lt; endl; cin &gt;&gt; number_of_flips; system("cls"); for(int i = 0; i &lt; number_of_flips; i++){ coin_flip = rand() % 2; if(coin_flip == 0){ cout &lt;&lt; "Heads" &lt;&lt; endl; if(coin_flip == previous_flip){ head_count = head_count + 1; } else{ if(head_count &gt; highest_head){ highest_head = head_count; } head_count = 0; } } if(coin_flip == 1){ cout &lt;&lt; "Tails" &lt;&lt; endl; if(coin_flip == previous_flip){ tail_count = tail_count + 1; } else{ if(tail_count &gt; highest_tail){ highest_tail = tail_count; } tail_count = 0; } } previous_flip = coin_flip; } cout &lt;&lt; "The longest run of heads is " &lt;&lt; highest_head &lt;&lt; endl; cout &lt;&lt; "The longest run of tails is " &lt;&lt; highest_tail &lt;&lt; endl; system("pause"); return 0; } </code></pre> <p>Here is an example of the output:</p> <pre><code>Tails Tails Tails Heads Heads Tails Tails Tails Tails Heads The longest run of heads is 1 The longest run of tails is 2 </code></pre> <p>As a reference, here is my final code that I believe works now:</p> <pre><code>int main(){ int number_of_flips; int coin_flip; int previous_flip = 2; int head_count = 0; int tail_count = 0; int highest_head = 0; int highest_tail = 0; srand(time(NULL)); cout &lt;&lt; "Enter the number of coin flips:" &lt;&lt; endl; cin &gt;&gt; number_of_flips; system("cls"); for(int i = 0; i &lt; number_of_flips; i++){ coin_flip = rand() % 2; if(coin_flip == 0){ cout &lt;&lt; "Heads" &lt;&lt; endl; if(coin_flip == previous_flip){ head_count = head_count + 1; } else{ if(head_count &gt; highest_head){ highest_head = head_count; } head_count = 1; } } if(coin_flip == 1){ cout &lt;&lt; "Tails" &lt;&lt; endl; if(coin_flip == previous_flip){ tail_count = tail_count + 1; } else{ if(tail_count &gt; highest_tail){ highest_tail = tail_count; } tail_count = 1; } } previous_flip = coin_flip; } if(head_count &gt; highest_head){ highest_head = head_count; } if(tail_count &gt; highest_tail){ highest_tail = tail_count; } cout &lt;&lt; "The longest run of heads is " &lt;&lt; highest_head &lt;&lt; endl; cout &lt;&lt; "The longest run of tails is " &lt;&lt; highest_tail &lt;&lt; endl; system("pause"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload