CS105: Introduction to Computer Programming: C++

Assignment #8: Super Freqs and Sets


Tuesday, March 31st at 11:59pm


This last assignment is easy. You'll write two functions for your BigInt class: one that returns a map containing the frequencies of each of the ten digits, and one that returns a set containing the digits in the number.


There are two functions for you to consider:

  1. Add a function to your BigInt class called digitFreqs. This function should take no arguments, and return a map containing 10 pairs of ints, one pair for each digit 0 through 9. The number associated with each digit should be the number of times that digit appears in the BigInt.

    For example, the number "11" has two 1's, and no other digits. So the frequency of 1 should be two, and the frequencies of all the other digits should be zero.

    Or, the number "1234567890" has one of each digit, so all of the frequencies should be one.

    After you've implemented your digitFreqs function, you should be able to use it like this:

      BigInt num1("999");
      map<int,int> freqs = num1.digitFreqs();
      cout << "zero appears " << freqs[0] << " times, " 
           << " whereas nine appears " << freqs[9] << " times." 
           << '\n';
      // should say zero appears 0 times, whereas nine appears 3 times.
      BigInt num2("1234555");
      freqs = num2.digitFreqs();
      for( int i = 0; i <= 9; i++ ) {
        cout << i << " appears " << freqs[i] 
             << " time" << (freqs[i] != 1 ? "s" : "") << '\n';
      // should display the frequencies for each digit: 
      //  0 appears 0 times
      //  1 appears 1 time
      //  2 appears 1 time
      //  3 appears 1 time
      //  4 appears 1 time
      //  5 appears 3 times
      //  6 appears 0 times
      //  7 appears 0 times
      //  8 appears 0 times
      //  9 appears 0 times
  2. Also add a function called uniqueDigits. This function should take no arguments and return a set of integers, one for each digit that occurs in the number.

    For example, the number "11" only has one kind of digit in it -- the digit 1. So the set that is returned should contain only one element, the digit 1.

    The number "1234567890" has one of each digit, so the resulting set should have all 10 possible digits in it.

    For example, you could test your uniqueDigits function with the following code:

      BigInt num3("5444412233333");
      set<int> digits = num3.uniqueDigits();
      cout << num3 << " contains the digits: ";
      for( set<int>::const_iterator iter = digits.begin(); iter != digits.end(); ++iter ) {
        cout << *iter << ' ';
      cout << '\n';
      // should display 
      // 5444412233333 contains the digits: 1 2 3 4 5 

Compiling and Running

As usual, you should be able to compile your code using the Makefile from the last assignment by typing:


If all goes well, you shouldn't see any error messages. Then you can run your code by typing:


Whatever you've included in your main function should run, and any output that is generated should appear on your screen.

Turning it in

When you're happy with your code, use the turnin program to submit your BigInt.cc and BigInt.h files. Use assignment8 as the assignment name.

Submission Checklist