Embercode
✕
ArraysQuick quiz

Counting and seen arrays

7 questions. Covers Boolean seen arrays.

Questions 1 to 5 of 7

  1. 1. Predict the output of this program. ``cpp #include <array> #include <iostream> #include <string_view> using namespace std; int main() { string_view word = "banana"; array<int, 26> counts{}; for (char c : word) { counts[c - 'a'] += 1; } for (int i = 0; i < 26; ++i) { if (counts[i] > 0) { cout << static_cast<char>('a' + i) << counts[i] << " "; } } cout << "\n"; } `` What does it print?
  2. 2. Predict the output of this program. It counts duplicates with a seen array. ``cpp #include <iostream> #include <vector> using namespace std; int main() { vector<bool> seen(6, false); vector<int> v = {5, 2, 5, 0}; int dups = 0; for (int x : v) { if (seen[x]) { ++dups; } else { seen[x] = true; } } cout << boolalpha << dups << " " << seen[2] << "\n"; } `` What does it print?
  3. 3. A SeenSet for the values 0 to 9 gets the inserts 7, 3, 7, 3 and 1, in that order. Then reset() is called. How many entries does reset() write?
  4. 4. Which declaration gives a seen array that is safe for the values 1 to 100?
  5. 5. In a 26-slot counts array indexed by c - 'a', the letter q uses index ____. Write the number.

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.