I have so store a very long value of type integer that can't be stored in a variable of long type. how can i store such a long integer value in a c programme. Please illustrate it through an example/ programme if possible.
views:
1082answers:
8I won't give you the code, but I can make a couple of suggestions for approaches to take:
- Try storing the value as a character string and converting to perform calculations
- Try breaking the value up into multiple integers representing a portion of the value
- Look up existing libraries that may take care of this for you
Good luck
This is a common question in introductory computer science classes at university. The primary areas of focus are a) understanding how (integer) numbers are stored as binary digits, and b) the basics of data structures, where if a programming language does not provide the desired data structure itself, you can use meta or collection structures, such as struct
in C, class
in C++, or record
in Pascal.
So how is a smaller integer stored in a computer? In C, you have data types char, short, int, long
that can all be used to store integers of various sizes. (I'll ignore long long
for this discussion.) Let's say for sake of generality that on a given 32-bit platform the sizes are 8-bit, 16-bit, 32-bit, and 64-bit respectively. Consider the values that can be represented (to simplify considered unsigned).
Now, how could you store a larger integer, that cannot be stored in an unsigned 64-bit long? Make your own large integer data type, comprised of multiple smaller (but standard) integers such that they represent larger values.
I think this should point you in the right direction, and enable you to write your own answer to your homework or exam question.
Think about storing a numbers as sequences of decimal digits using a struct like this:
struct num {
int ndigits;
char d[MAXDIGITS];
};
For example, the number 123456 could be initialized as
struct num n = { 6, { 6, 5, 4, 3, 2, 1 } };
The reversed digit order turns out to be important for easy calculation. In particular, the place value of n.d[i] is n.d[i] * 10^i.
Now, a few questions:
- How would you add one to a
num
? - How would you add an arbitrary single digit to a
num
? - How would you add two
num
s together? - How would you multiply a
num
by two? - How would you multiply a
num
by a single digit? - How would you multiple two
num
s together? HINT: Do some pencil and paper multiplications and see how they work.
If you work through this sequence of questions, you should be able to write a function for each step, and re-use those functions to answer the later questions, and end up with a very simple and unoptimized long (well, up to MAXDIGIT
digits) integer package for addition and multiplication of positive numbers.
Other questions:
- How do you generalize
num
to represent negative numbers as well as positive? - How do you divide one num by another (ignoring remainders)? This is trickier than multiplication, but again, start by doing a few pencil and paper long divisions and think carefully about what you do.
If it's only for display, I would suggest a <stdio.h>
(for the infamous printf) from the c standard library or maybe the <string.h>
to make some modification.
struct digitcontainer
{
struct digitcontainer* left;
struct digitcontainer* right;
unsigned char digit;
}
struct longinteger
{
char sign;
struct digitcontainer* firstdigit;
}
// positive number with 1000 digits
void test()
{
struct longinteger myNumber;
myNumber.sign = '+';
myNumber.firstdigit = (struct digitcontainer*)malloc( sizeof(digitcontainer) );
myNumber.firstdigit->left = NULL;
myNumber.firstdigit->right = NULL;
myNumber.firstdigit->digit = 1;
struct digitcontainer* left = myNumber.firstdigit;
for( int i=1; i<1000; i++ )
{
left->right = (struct digitcontainer*)malloc( sizeof( digitcontainer ) );
left->right->left = left;
left->right->digit = (unsigned char)i;
left = left->right;
}
left->right = NULL;
// call free for each digitcontainer you are finished using the number
}
LibTomMath provides a nice arbitrary precision integer implementation in C, I was able to drop it into an iPhone project with almost no modification.