You do not need any characteristic of C ++, so I will limit myself to C (by the way, I differentiate my response from the previous ones, contributing something new).
To be able to read a keyboard number, we will use scanf()
. This function allows a number taken from the keyboard to be saved in an integer variable, such as: int num; scanf( "%d", &num )
. It is important to remember to put an ampersand in front of the variable, as it will be modified within scanf()
. The opposite of scanf()
is printf()
, which allows you to display a value per console. Both functions accept a string of characters to know the format of the value to be displayed or to be requested by keyboard, as the first parameter. %d
represents an integer number, equivalent to type int
.
To save the results, we need a data structure that allows us to store the result of the count for each number. We will need to be able to ask: for 5 ?, and to return us: 1 time (or whatever they are). You also need to be able to say: "increase the count by 1 to 5." That is, we need to be able to associate an integer value (the count) with another integer value (a digit from 0 to 9). In C ++ it is possible to use a map<>
, but in the specific case of associating an integer value with another integer value, we can use a simple vector, a primitive array. If we create: int digitos[10]
, then we have 10 integer values to save the character count: one in position 0, another in position 1 ... so, the position in the vector (the index) represents the digits, while the value of the position, the count. Of course, we will have to initialize each position of the vector to zero before using it.
Finally, we need to be able to access each number of the number that the user has entered by keyboard. One way to do this is to convert the number to a character string, and then access each position and post it. But this implies reserving memory, when we can do the same with simple calculations: suppose we have the number 482. If we divide the number by 10, we have 48. The rest will be 2. If we divide 48 by 10, the result is 4, while the rest is 8. If we divide the number again by 10, the result is 0, the rest is 4. With the remains of the divisions, we have "walked" each number of the number, though, in the opposite direction, although for count the appearances of each figure that does not matter. The only thing to keep in mind is that the number must be positive, so it will be necessary to call abs()
.
Of course, to do all this it is necessary to use loops, which allow repeating the execution of a body of instructions several times. for()
is used when you previously know the number of times that body of instructions will have to execute. while()
is used when the number of times to repeat that code is not known, so it is accompanied by a condition, and the body of instructions will be executed while the condition is met (return true
).
void cuenta_ocurrencias_digitos(int n, int digitos[])
{
// Inicializa
for(int i = 0; i < 10; ++i) {
digitos[ i ] = 0;
}
// Cuenta las ocurrencias
n = abs( n );
while( n > 0 ) {
digitos[ n % 10 ] += 1;
n /= 10;
}
}
The previous function initializes the vector digitos
, and then decomposes the number in figures, increasing by one the position in the vector for each of them.
It's all left to show the results. The only relevant results are the counting of figures when said counting is greater than zero, as seen in the following function.
void muestra_resultado(int n, int digitos[])
{
printf( "\nPara: %d\n", n );
for(int i = 0; i < 10; ++i) {
if ( digitos[ i ] > 0 ) {
printf( "%d aparece %d veces.\n", i, digitos[ i ] );
}
}
printf( "\n" );
}
I hope all this helps you. You have the full code in IDEOne .