I made a program that decomposed a number entered an arrangement [...] I wanted to know if this can be done more easily.
Yes, the code could be much simpler, but before sharing my proposal I will indicate some details of your code:
- Headers
<stdio.h>
and <stdlib.h>
are from c not from c ++ . These headers have a version adapted to C ++ that has the prefix c
and has no extension. If you really need to use the C headers (which will never be the case) you should use the C ++ equivalents <cstdio>
and <cstdlib>
. Read this thread to find out why.
- There is no obligation to use the
using namespace std;
clause since it is only an aid to the writing of code; If you decide to use this clause do not do it in the global scope, use it in the smallest possible scope. Read this thread to find out why.
- You do not have to " clean your fix " in a loop because thanks to the initialization that you applied (
arreglo[15]={0}
) all its elements are already 0
. Read this thread to find out why.
- Avoid abusing
std::endl
(because it can cause performance problems) and favors the use of the explicit line break ( \n
). Read this thread to find out why.
Having said that, I propose:
Proposal.
To separate a decimal number in its digits it is enough to obtain its residual to divide between the base (in this case decimal base):
// Mientras el número no sea 0, extrae sus dígitos:
while (numero)
{
auto digito = numero % 10;
std::cout << digito;
numero /= 0;
}
The operator %
returns the remainder of an entire division, so 111 % 10
is 1
, 42 % 10
is 2
, 112357 % 111
is 25
, etc ...
The problem is that this algorithm extracts the digits from the least significant to the most significant (that is: in reverse order) you have solved it starting with the largest divisor that has occurred to you ( 100000000
), we can solve this problem using recursion:
void digito_a_digito(int numero, int (&destino)[15], int &indice)
{
if (numero)
{
digito_a_digito(numero / 10, destino, indice);
destino[indice++] = numero % 10;
}
}
Since inserting the value in destino
becomes after of the recursive call, the recursion will appear to be the reverse: In the index 0
the last digit will be placed, in the 1
the penultimate, etc ... the parameter destino
is a reference to an array of 15 positions (which will be where we save the result) and the parameter indice
is the one that tells us in which position of destino
we must write, we pass it as a reference so that its value can be modified in all calls, therefore:
int main()
{
int resultado[15] = {0};
digito_a_digito(987654321, resultado);
for (const auto &v : resultado) std::cout << v;
return 0;
}
The previous code shows:
987654321000000
You can see that there are several zeros after the last digit, since the arrangement is of 15 positions and the number of 9 digits, changing the program slightly we fix it:
int main()
{
using namespace std;
// Captura de un numero
int numero;
cout << "\t\tNUMERO-ARREGLO\n\nteclea un numero=";
cin >> numero;
cout << '\n';
int indice = 0, resultado[15] = {0};
digito_a_digito(numero, resultado, indice);
for (int indice = 0; numero; numero /= 10, ++indice)
cout << resultado[indice];
return 0;
}
In the loop for
of the previous code, we put the index back to 0
and make the condition of completion of the loop is that the number read is different from 0
, if we divide by 10 in each turn of the loop, the loop will give as many turns as digits (in base 10) have, in each round we also increase the index to know what position of the array to print.