Q:

What happens in the program presented in this section if the input values are all equal? What if there are no duplicated values?

0

What happens in the program presented in this section if the input values are all equal? What if there are no duplicated values?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>

int main() {
  int curVal = 0, val = 0;
  if (std::cin >> curVal) {
    int cnt = 1;
    while (std::cin >> val) {
      if (val == curVal)
        ++cnt;
      else {
        std::cout << curVal << " occurs "
                  << cnt << " time(s)" << std::endl;
        curVal = val;
        cnt = 1;
      }
    }
    std::cout << curVal << " occurs "
              << cnt << " time(s)" << std::endl;
  }
  return 0;
}

 

Output:

3 7 8 5 3 3 4 8 
3 occurs 1 time(s)
7 occurs 1 time(s)
8 occurs 1 time(s)
5 occurs 1 time(s)
3 occurs 2 time(s)
4 occurs 1 time(s)

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now