fork download
  1. #include <iostream>
  2.  
  3. int main() {
  4.  
  5. int a[] = {1, -2, 3, -4, 5, -6, 0, 4, -3, 21, 0, 19, -5};
  6. const int len = sizeof(a)/sizeof(*a);
  7. int pcnt = 0;
  8. int zsum = 0;
  9. bool zfound = false;
  10. for( int i=0; i<len; ++i )
  11. {
  12. if( a[i] > 0 ) pcnt++;
  13. if( a[i] == 0 )
  14. {
  15. zfound = true;
  16. zsum = 0;
  17. }
  18. if( zfound ) zsum += a[i];
  19. }
  20. std::cout << "Count of positive elements: " << pcnt << std::endl;
  21. std::cout << "Sum of elements after last zero: " << zsum << std::endl;
  22. return 0;
  23. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Count of positive elements: 6
Sum of elements after last zero: 14