samedi 4 avril 2020

C++ How to find the number of occurances of each digit in a random integer?

I've made a simple random program that generates a random number between 0 and 9

int main(int argc, char* argv[])
{
        int n;
        srand(time(NULL));
         n = rand() % 10;  /* generate a number from 0 - 9 */

        for ( int i=0; i < n; i++ )
                cout << rand() << endl;

         //count frequency of digits


        getch();
        return 0;
}

The next stage is to set up an integer array to count how many of each digit occurs. For example if the random numbers are 144566888, then the output would be "1 appears 1 time 4 appears 2 times 5 appears 1 time 5 appears 2 times 8 appears 3 times"

Im unsure on how to do this. Thankyou




Aucun commentaire:

Enregistrer un commentaire