Thread: long integers

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    8

    long integers

    I am trying to manipulate very big, long integers for one of my programs, while experimenting with a simple program that reads in a number then prints it back out I found out that int variables start to go mad if you go to high.
    I though I might be able to store integers in an array instead of an int variable. But having only just started out I do not know if it is possible to read in the digits one at a time or even if arrays allow you to store numbers the way I want.

    Any suggestion would be appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, it can be done. A starting point that you can use is to think of the algorithms that you might use when doing those calculations on paper, then implement them for your array.

    If you actually just want to use a bignum library, search the Web, or use a well known one like GNU MP.
    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
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just a reminder first, that an unsigned long, or unsigned long long, int data type, can be larger than you might think.

    Read the header file "limits.h" to see the range of integers your compiler supports. Don't change any values in there, just read.

    You can "peel" off the right most digit of any integer, one at a time, using this algorithm:

    Code:
    get theNumber
    
    while (theNumber > 0) {
       rightMost digit equals theNumber % 10  //10 is the number base
       theNumber = theNumber / 10  //removes the rightmost digit 
    }
    Working with a digit, if you want to change it to a char, you can simply add '0' to it. A number 1 is not the same as a char of '1', of course.

    There are also special functions that can help change a number into a string and vice versa.

    Although char's are a more compact data type for working with digits, I like using int arrays, for what you are looking to do.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why would you do that when you can just read the number as text. It might be annoying but you can definitely do regular math with a string type if you don't want to use something like gmp.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Thank you
    I have have just started programming so I do not know what you can do in C, I did not know that there was an easy way to find out my compilers max integer size.
    I was thinking of putting the digits in reverse so 23 would be [3][2]that way when I add integers together if a digit goes over 10 I can add one to the next part of the array just the same as on paper.
    I did not know there was a big number library, it may be easier that what I have in mind so I will have to look it up.
    I want the integers to be exact but easy to manipulate in the code so I can do things like


    58973475983427895743985787589734758934275843798574 8759874589734857348957894759843745978348975
    + 74598437958347895748937589475894738597348975934857 8394758947589475984379857489578349758943

    = whatever

    in the code but without doing every part of the sum myself.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would have him know how to "peel" digits off a number, just because it's a valuable trick to know - maybe he'll find it useful, because the assignment requires it. Assignments can be odd things, imo.

    "Doing every part of the sum yourself"? Huh?? You use a for or a while loop, and indeed, you can go through the entire arrays of digits, just like on paper, inside that loop.

    If it was any easier it would assume a life form of it's own, and try to take over the world. <grin>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding Long Integers!
    By alireza beygi in forum C Programming
    Replies: 1
    Last Post: 12-17-2011, 07:11 AM
  2. Long integers not long enough
    By cnewbie1 in forum C Programming
    Replies: 14
    Last Post: 10-31-2010, 04:32 AM
  3. which command can be used instead of enum for long integers?
    By aarti_gehani in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 04:05 AM
  4. Long Integers
    By SimonM in forum C Programming
    Replies: 25
    Last Post: 07-25-2007, 01:57 PM
  5. Long Integers
    By kabuatama in forum C Programming
    Replies: 24
    Last Post: 01-28-2006, 01:21 PM