Thread: Storing an int as individual digits in an array

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Storing an int as individual digits in an array

    I have an int i, say 6473, and I want to store each digit separately in an array, like so:

    array[0] = 6;
    array[1] = 4;
    array[2] = 7;
    araay[3] = 3;

    How can I do this?

    thx

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is the problem, I would ask? You specifically mention an array - is the problem that you do not know how to create or declare an array?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    I know how to declare an array, I just don't know how to split an int into an array.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, now I understand. So basically, each digit into its own array. For that I would ask if you actually answer laserlight's question.
    I could also provide some insight - how would you do it mathematically in the real world?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Quote Originally Posted by Elysia View Post
    Ah, now I understand. So basically, each digit into its own array. For that I would ask if you actually answer laserlight's question.
    I could also provide some insight - how would you do it mathematically in the real world?
    No that's not what I meant.

    Think of it this way, ignore the whole array part, the question really is:

    How would I access each individual digit of the number: 45656

    i.e. what would I have to do to access the 4?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How would I access each individual digit of the number
    Suppose you divide the number by 10. What is the remainder?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Quote Originally Posted by laserlight View Post
    Suppose you divide the number by 10. What is the remainder?
    Ah k, in the end I've just decided to convert to a string

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Admittedly, it's possible without a string, but a bit tricky. Here's an example!
    Code:
    	const int mynum = 12345;
    	int num1 = ( mynum - (mynum / 10 * 10) ) / 1;
    	int num2 = ( mynum - (mynum / 100 * 100) ) / 10;
    	int num3 = ( mynum - (mynum / 1000 * 1000) ) / 100;
    	int num4 = ( mynum - (mynum / 10000 * 10000) ) / 1000;
    	int num5 = ( mynum - (mynum / 100000 * 100000) ) / 10000;
    num1 = 5, num2 = 4, num3 = 3, num4 = 2, num5 = 1
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Might I suggest sprintf()...
    Code:
    char str[12];
    int num = 12345;
    sprintf( str, "%d", num );

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, it's not very difficult to do your own as long as you store the string from the back. But obviously much less code to use sprintf() [at least of code "that you write yourself"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Arguably, the easiest way is to convert to string first, then take each element and covert back to integer again. Although the OP did specify that it was converted to a string in the end.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM