#include <stdio.h> #include <stdlib.h> #include <math.h> char * convertNumberIntoArray(unsigned int number) { unsigned int length = (int)(log10((float)number)) + 1; char * arr = (char *) malloc(length * sizeof(char)), * curr = arr; do { *curr++ = number % 10; number /= 10; } while (number != 0); return arr; } int main(){ int inputNum1; int inputNum2; //greet user and get input printf("hello please enter an integer no bigger than 512 digits: "); scanf("%d", &inputNum1); printf("please enter a number less than the first please: "); scanf("%d", &inputNum2); convertNumberIntoArray(inputNum1); getchar(); getchar(); return 0; }
4 Replies  11878 Views  Last Post: 08 June 2011  03:32 PM
#1
adding and subtracting large numbers using arrays
Posted 08 June 2011  02:04 PM
Okay so what I'm trying to do is add and subtract two numbers in arrays and this is what i have so far i don't know where to go from here I've been googling for a while now and tried a few things out but nothing has really worked out the numbers should be able to go up to 512 characters
Replies To: adding and subtracting large numbers using arrays
#2
Re: adding and subtracting large numbers using arrays
Posted 08 June 2011  02:17 PM
Think about it. If you have two numbers, one two digits and one one digit for the sake of example, you would just have to get the rightmost digit in the arrays, convert them to numbers, add them, and put that in the rightmost of a new array to hold the answer.
So, eg : (pseudocode)
EDIT:
it would also be highly recommended to know the length of the arrays, and completely necessary to free the memory you allocate!
So, eg : (pseudocode)
1st digit 2nd digit 1 2 + 5 = ( 1 + 0 ) ( 2 + 5 )
EDIT:
it would also be highly recommended to know the length of the arrays, and completely necessary to free the memory you allocate!
This post has been edited by sk1v3r: 08 June 2011  02:18 PM
#3
Re: adding and subtracting large numbers using arrays
Posted 08 June 2011  02:28 PM
Are you talking about Arbitraryprecision arithmetic?
If so I started helping someone out on writing a BigInt library once and hereis my initial effort.
In another posts a user was looking for "unbounded numbers" and I gave some advice on where to begin:
If you are not looking for arbitrary precision numbers please ignore everything I said here
If so I started helping someone out on writing a BigInt library once and hereis my initial effort.
In another posts a user was looking for "unbounded numbers" and I gave some advice on where to begin:
NickDMax, on 26 June 2010  09:36 AM, said:
First of all  there is no such thing as an "unbounded number" in a computer, a computer is finite and can only do finite things and so even if you make the number very very very large, it still has a bound. Even if the computer could never "count" to that number withing the age of the universe (which really isn't that hard to do) the number itself is still bounded even if the computer can not count up to it.
So the term you are looking for is "arbitrary precision" or "big numbers".
Probably the best place to start is with Knuth. The Art of Computer Programming. Vol 2: Seminumerical Algorithms has a chapter called "The Classical Algorithms" which covers how to do arbitrary precision arithmetic. Of course these ARE the classical algorithms and so there are more modern versions of multiplication that Knuth alludes to but does not demonstrate in the book. Of course the problem with Knuth's books are that much of the material you are supposed to learn is in the exercises. You should be able to find Knuth's books at the local university library (and they usually have a couple of copies).
Another good reference book for this would be one called BigNum Math: Implementing Cryptographic Multiple Precision Arithmetic  however this is kind of tied up with an open source library and so it helps if you download the library as well because all of the names he will be using come from the library. However, it really is not a bad book.
I began a really basic BigInteger class one for a kid who was trying to calculate phi here  looking back over it I would NEVER do it that way now... but its a beginning.
So the term you are looking for is "arbitrary precision" or "big numbers".
Probably the best place to start is with Knuth. The Art of Computer Programming. Vol 2: Seminumerical Algorithms has a chapter called "The Classical Algorithms" which covers how to do arbitrary precision arithmetic. Of course these ARE the classical algorithms and so there are more modern versions of multiplication that Knuth alludes to but does not demonstrate in the book. Of course the problem with Knuth's books are that much of the material you are supposed to learn is in the exercises. You should be able to find Knuth's books at the local university library (and they usually have a couple of copies).
Another good reference book for this would be one called BigNum Math: Implementing Cryptographic Multiple Precision Arithmetic  however this is kind of tied up with an open source library and so it helps if you download the library as well because all of the names he will be using come from the library. However, it really is not a bad book.
I began a really basic BigInteger class one for a kid who was trying to calculate phi here  looking back over it I would NEVER do it that way now... but its a beginning.
If you are not looking for arbitrary precision numbers please ignore everything I said here
#4
Re: adding and subtracting large numbers using arrays
Posted 08 June 2011  02:45 PM
sorry i wasn't really being clear in my first post
this is in C coding
im suppose get the user to give me a number up to 512 digits
then take in a second number up to 512 digits
and then add them together then subtract one from another
sorry again this is my first time posting something
this is in C coding
im suppose get the user to give me a number up to 512 digits
then take in a second number up to 512 digits
and then add them together then subtract one from another
sorry again this is my first time posting something
This post has been edited by tsukeme: 08 June 2011  02:46 PM
#5
Re: adding and subtracting large numbers using arrays
Posted 08 June 2011  03:32 PM
Oh and just to add to my previous post: I would definitely recommend taking advantage of operator overloading for this one.
Ignore everything I just said sorry, I didn't read the post above and realise that this is a C project...
This post has been edited by RetardedGenius: 08 June 2011  03:34 PM
Page 1 of 1
