// distinct1000: print the number of distinct numbers between 0 and 999 appearing // in the input // For instance, "5 8 8 4 8 5" has three distinct input values (4, 5, and 8). // // Robert W. Hasker, March 2016 // #include using namespace std; constexpr int POSSIBLE_INPUTS = 1000; // Reads integers between 0 and POSSIBLE_INPUTS - 1 and marks each distinct // integer read. numbers_found must be allocated with POSSIBLE_INPUTS items. void read(bool numbers_found[]) { for(int i = 0; i < POSSIBLE_INPUTS; ++i) numbers_found[i] = false; int num; cin >> num; while ( cin ) { numbers_found[num] = true; cin >> num; } } // Returns the number of true's in numbers_found where numbers_found has // POSSIBLE_INPUTS items. int trueCount(bool numbers_found[]) { int count = 0; for(int i = 0; i < POSSIBLE_INPUTS; ++i) { if ( numbers_found[i] ) ++count; } return count; } // main program: reads numbers and prints the count int main() { bool numbers_found[POSSIBLE_INPUTS]; read(numbers_found); int distinct_integers = trueCount(numbers_found); cout << "Number of distinct integers: " << distinct_integers << endl; return 0; }