c++ - maximum consecutive repetition in an array -


i want count maximal consecutive trues in array. in follows, return me 4. seems trues in array added up. what's wrong work? here code.

int main() { int maxcount = 0; bool list[7]; list[0] = true; list[1] = true; list[2] = false; list[3] = false; list[4] = true; list[5] = true; list[6] = false;  (int = 0; < 6; i++) {     int count = 0;     if (list[i]==true)     {         count++;         (int j = + 1; j < 7; j++)         {             if (list[j]== list[i])             {                 count++;             }         }     }     if (count > maxcount)     {         maxcount = count;     } } cout << maxcount << endl; } 

the way implementing wrong. adding true entries in array, without accounting false in between.

do this:

int currentcount = 0, maxcount = 0; (int = 0; < 7; ++i) {     // arr name of array     if(arr[i])    ++currentcount;     else    currentcount = 0;  // resetting 0 if false encountered     maxcount = ( (currentcount > maxcount) ? currentcount : maxcount ); } cout << maxcount << endl; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -