0

I have been given some integers and I have to count the frequency of a specific digit in the number. example input:

5
447474
228
6664
40
81

The first number says number of integers in the list. I am finding frequency of 4 in this case. I tried to change the integer to an array, but it is not working.


#include<iostream>
#include<cmath>
#include<vector>

using namespace std;

int main() {
    
    int n;
    cin>>n;
    
    for (int i=0; i<n; i++)
    {
       int x;
       cin>>x;
       int frequency=0;
       int t=log10(x);
       int arr[t];
       for (i=t; i>0; i--)
       {
          arr[i]=x%10;
          x=x/10;
       }
       for(int i=0; i<t; i++)
       {
           if(arr[i]==4)
           {
               frequency++;
           }
           
       }
       
     std::cout << frequency << std::endl;
        
    }
    return 0;
}

  • 1
    what is the meaning of "not working"? – 463035818_is_not_an_ai Jul 27 '21 at 18:35
  • btw its a little ironic that you tagged `string` because using a string would greatly simplify the task, but you arent using a string – 463035818_is_not_an_ai Jul 27 '21 at 18:36
  • 1
    `int arr[t];` is not standard c++. [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). In general you should use a `std::vector` for dynamically sized arrays, but here you can use a `std::string` – 463035818_is_not_an_ai Jul 27 '21 at 18:37
  • `int arr[t];` is also completely unnecessary. Instead of storing each digit in an array, why not just check if the digit is `4`? – Drew Dormann Jul 27 '21 at 18:43

2 Answers2

2

No need to create an array, or to determine the number of digits. Just loop until the number reaches zero.

int digitCount(int n, int d) {
    if(n < 0) n = -n;
    int count = 0;
    for(; n != 0; n /= 10)
        if(n % 10 == d) count++;
    return count;
}

Test:

cout << digitCount(447474, 4) << endl;
cout << digitCount(-447474, 4) << endl;

Output:

4
4
RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16
0

Your code uses VLAs which are not standard C++. See Why aren't variable-length arrays part of the C++ standard?.

log10(x) is not the number of digits. For example log10(1234) == 3.09131516 but it is 4 digits. Also you are accessing the array out of bounds in the first iteration of the loop: arr[t]. Valid indices in an array of size t are 0,1,2,...,t-1. Trying to access arr[t] is undefined behavior.

Actually you dont need any array. Instead of storing the digits in an array you can immediately check whether it is a 4 and count.

Even simpler would be to read the user input as a std::string:

#include <string>
#include <algorithm>
#include <iostream>

int main() {
    std::string input;
    std::cin >> input;
    std::cout << std::count(input.begin(),input.end(),'4');
}

Perhaps you should add some checks to verify that the user input is actually a valid number. However, also when reading an int you should validate the input.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • One problem with reading input as a string is that not all strings contain a representation of integral values. Consider the string `"X42A4"`. So it is necessary to add parsing code, and somehow handle cases where the user gives bad input. Also, using a string presumes that there is no value in basic mathematics, if the program has already read/computed an integral value - converting to a string in such a case is unnecessary. – Peter Jul 28 '21 at 03:28
  • @Peter the necessity to check for valid input isnt much different whether a string or an `int` is read. OPs code isnt doing any checking either. I suppose this is for an exercise where one can assume valid input. Nevertheless you are of course right. Will add a note – 463035818_is_not_an_ai Jul 28 '21 at 07:58