1

I need some advice on an assignment that asks to write a function to find the even digit that has the largest/smallest occurrence.

My output should look like:

How many integers (to be worked on) ? 2
  Enter integer #1: 1230476
  Enter integer #2: 10034850

Occurrence of all existing digits --
    Digit 0 : 4
    Digit 1 : 2
    Digit 2 : 1
    Digit 3 : 2
    Digit 4 : 2
    Digit 5 : 1
    Digit 6 : 1
    Digit 7 : 1
    Digit 8 : 1
    Digit 9 : 0
Occurence of all existing EVEN digits --
    Digit 0 : 4
    Digit 2 : 1
    Digit 4 : 2
    Digit 6 : 1
    Digit 8 : 1

The even digit(s) that has/have the largest occurrence - 0

And the number of occurrence(s) : 4

The even digit(s) that has/have the smallest occurrence - 2 6 8 And the number of occurrence(s) : 1

This is my code so far... I cannot seem to get the last part of finding the largest/smallest occurrence..

This is my code thus far: void displayDigitInfoUpdateStanDeng() {

  int intsWorkedOn;
  int* intValue;
  int allDigitCount[10] = {0};
  int largestOccurEven;
  int smallestOccurEven;
  int curDigit;

  cout << "\n  Calling on displayDigitInfoUpdateStanDeng() --"
   << "\n    How many integers (to be worked on) ? ";
  cin >> intsWorkedOn;

  intValue = new int[intsWorkedOn];

  for (int i = 0; i < intsWorkedOn; i++) {

    cout << "      Enter integer #" << i + 1 << ": ";
    cin >> *(intValue + i);
  }

  for (int i = 0; i < intsWorkedOn; i++) {

    do {

       allDigitCount[*(intValue + i) % 10]++;

   *(intValue + i) /= 10;
   } while (*(intValue + i));
  }

 cout << "\n    Occurence of all existing digits --";

 for (int i = 0; i < 10; i++) {


  cout << "\n        Digit " << i << " : " << allDigitCount[i];
}

      cout << "\n    Occurence of all existing EVEN digits --";

  for (int i = 0; i < 9; i++) {

     cout << "\n        Digit " << i - 1 << " : " << allDigitCount[i++];
}

 cout << "\n   The even digit(s) that has/have the largest occurrence -";

 for (int i = 0; i < 9; i++) {


   largestOccurEven = allDigitCount[i++] % 10;

   curDigit = allDigitCount[i++];

    if (curDigit < largestOccurEven) {
      cout << "\n    " << i
        << "\n And the number of occurrence(s) : " << largestOccurEven;
    } else {
      cout << endl;
    }
  }

This is my current output:

How many integers (to be worked on) ? 2 Enter integer #1: 1230476 Enter integer #2: 10034850

Occurrence of all existing digits --
    Digit 0 : 4
    Digit 1 : 2
    Digit 2 : 1
    Digit 3 : 2
    Digit 4 : 2
    Digit 5 : 1
    Digit 6 : 1
    Digit 7 : 1
    Digit 8 : 1
    Digit 9 : 0
Occurence of all existing EVEN digits --
    Digit 0 : 4
    Digit 2 : 1
    Digit 4 : 2
    Digit 6 : 1
    Digit 8 : 1

The even digit(s) that has/have the largest occurrence - 2

And the number of occurrence(s) : 4

The even digit(s) that has/have the smallest occurrence - ? And the number of occurrence(s) : 0

I am so confused... Why does it display i as 2 for the largest occurrence? I really need help!

1 Answers1

0

Doing i++ like that inside your for loop means you're looking in different "bins" at each of these steps:

largestOccurEven = allDigitCount[i++] % 10;

curDigit = allDigitCount[i++];

You'll want to avoid doing that. For instance, change both to simply i and change the for loop appropriately:

for (int i = 0; i < 9; i += 2) {
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Ah yes, that makes more sense. But now I'm not sure what to do with my if statement. I need it to output only the largest, but now the values for curDigit and largestOccurEven are the same... Would you mind giving me one more hint? – user2809589 Oct 08 '13 at 00:26
  • Nevermind! I changed to the if statement to: if (largestOccurEven > intsWorkedOn). Thank you so much!! – user2809589 Oct 08 '13 at 00:44