Displaying relevant values from an array

My task is to create a program that allows the user to input int values. It will then count the occurrence of each digit (i.e. how many 0's, 1's, etc.) in the set of ints and output various statistics. First and foremost, it will display the occurrence of each digit. However, I do not want it to display an occurrence of zero, this is where I'm running into issues. For example, I do want want it to print "Digit 2 : 0" if there are no 2's. Below is my function. The first part deals with counting the occurrence of each digit, the second part is for printing the required output. For some reason the "if (ary[i] != 0)" is causing issues. Probably something very basic, but please help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void displayDigitInfo(int ary[], int intNumber)  {
  int intCount[10] = {0};
  int data;
  
  for (int i = 0; i < intNumber; i++) {
    data = (ary[i] < 0) ? -ary[i] : ary[i];
    do  {
      intCount[data % 10]++;
    
      data /= 10;
    } while (data != 0);
  }
  
  cout << "\nOccurrence of all existing digits –-" << endl;
  for (int i = 0; i < 10; i++)  {
    if (ary[i] != 0) {
      cout << "Digit " << i << " : " << intCount[i] << endl;
    }
  }
Why are you checking ary but outputting values from intCount?
Last edited on
Topic archived. No new replies allowed.