1

Suppose I have the integer 1004. I want to store this in the array A with the following pattern:

    A[0]=1
    A[1]=0
    A[2]=0
    A[3]=4

How can I get the value at that index ? How can I do this in C++?

BlueTune
  • 1,023
  • 6
  • 18
heybud
  • 11
  • 1
  • 1
    SO is a site to help you with specific problems. The site guidelines actually requires you to try and solve the problem first and show how and why you failed. Please read on how to construct a [mcve]. – super Feb 21 '20 at 10:30
  • 'that index' what index? – Stack Danny Feb 21 '20 at 10:32
  • You mean to split a number into it's digits? Then [this question](https://stackoverflow.com/questions/1397737/how-to-get-the-digits-of-a-number-without-converting-it-to-a-string-char-array) may be helpful. Or you simply convert it to a string. – Lukas-T Feb 21 '20 at 10:33
  • 1
    Does this answer your question? [How do I split an int into its digits?](https://stackoverflow.com/questions/4261589/how-do-i-split-an-int-into-its-digits) – dfrib Feb 21 '20 at 10:39

5 Answers5

1

You get the last index of a number by using modulo 10 and then remove that value by dividing the number by 10.

So assume you do this:

1004 % 10 = 4
1004 / 10 = 100

Then repeat that for each digit

DJSchaffner
  • 562
  • 7
  • 22
  • note, this doesn't store the values as OP requested – Stack Danny Feb 21 '20 at 10:43
  • That is just a matter of putting the obtained values in an array in reverse order but I figured OPs main problem was obtaining them. If he really doesn't know how to do that I might update the answer – DJSchaffner Feb 21 '20 at 10:45
1

Using static memory:

int originalNumber = 1004;
int digitArray[10] = {0};
int idx = 0;

while (originalNumber > 0)
{
    int digit = n % 10;
    originalNumber /= 10;
    digitArray[idx] = digit;
    ++idx;
}

// Reverse the order of the array
std::reverse(std::begin(digitArray), std::begin(digitArray)+(idx-1));
David
  • 165
  • 1
  • 8
0

I'm not sure if it's the most efficient way of doing this but it definitely works.

You can enter each digit in the number to the array but from the end of the array to the beginning.

For example, there is an array with the size of 4, so you get the last digit of the number like this: num % 10, and push the digit to the third index of the array.

Code example:

#define SIZE 4

int* numToArray(int num)
{
    int* arr = new int[SIZE]; // assuming you already know the number of digits in the number
    for(int i = SIZE-1; i >= 0; i++)
    {
        arr[i] = num % 10; // Enters the last digit to the array
        num /= 10; // Gets rid of the last digit in the number
    }
    return arr;
}
xBm
  • 69
  • 8
0

Instead of ordinary integer array, I suggest you using std::vector instead.

Then, you can have something like the following:

#include <iostream>
#include <vector>

int main() {
    int number = 1004;
    std::vector<int> digits;

    while (number != 0) {
        digits.insert(digits.begin(), number % 10);
        number /= 10;
    }

    for (auto const i : digits) {
        std::cout << i << " "; // 1 0 0 4
    }

    // or by index

    std::cout << std::endl;

    std::cout << "[0]" << digits[0] << std::endl; // 1
    std::cout << "[1]" << digits[1] << std::endl; // 0
    std::cout << "[2]" << digits[2] << std::endl; // 0
    std::cout << "[3]" << digits[3] << std::endl; // 4

    return 0;
}

Demo

NutCracker
  • 11,485
  • 4
  • 44
  • 68
0

Adding to all the existing answers I'd like to propose a more elegant, if probably less efficient approach utilizing the many wonders of the standard library.

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

int main()
{
    auto number = 1004;
    auto asString = std::to_string(number); // 
    std::vector<int> digits(asString.length());
    std::transform(asString.begin(), asString.end(), digits.begin(), [](char c){return c -'0';});

    for(auto d : digits)
    {
        std::cout << d << ' ';
    }
}
Lukas-T
  • 11,133
  • 3
  • 20
  • 30