How do I decompose a number in an array in c ++?

1

I made a program that decomposed a number entered an arrangement as long as it was static, it is this:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
using namespace std;
int main(){
    int numero=0, arreglo[15]={0}, i=0, divisor=100000000, digito=0, sustraendo=0;
    //captura de un numero
    cout<<"\t\tNUMERO-ARREGLO\n\n";
    cout<<"teclea un numero=";
    cin>>numero;
    cout<<endl;
    //limpiar arreglo
    for(i=0; i<15; i++){
        arreglo[i]=0;
    }
    //obtener digitos
    for(i=0; i<9; i++){
        digito=numero/divisor;
        arreglo[i]=digito;
        sustraendo=digito*divisor;
        numero=numero-sustraendo;
        divisor=divisor/10;
    }
    //imprimir arreglo
    for(i=0; i<15; i++){
        cout<<arreglo[i]<<" ";
    }
    return 0;
}

I wanted to know if you can do this more easily, that is, enter the number and put each element within the array as with a strcpy if you were using a string of characters since you wanted to do this same program but with a fix dynamic.

    
asked by Saul Sanchez 02.10.2018 в 20:53
source

2 answers

1

As you can see it here , you can do it with the following function:

#include <iostream>
#include <vector>
using namespace std;

vector <int> integerToArray(int x)
{
    vector <int> resultArray;
    while (true)
    {
        resultArray.insert(resultArray.begin(), x%10);
        x /= 10;
        if(x == 0)
            return resultArray;
    }
}

int main()
{
    vector <int> temp = integerToArray(1234567);
    for (auto const &element : temp)
        cout << element << " " ;
    return 0;
}

Greetings!

    
answered by 02.10.2018 в 21:42
1
  

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 not from . 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.

    
answered by 03.10.2018 в 08:58