# adding and subtracting large numbers using arrays

Page 1 of 1

## 4 Replies - 20128 Views - Last Post: 08 June 2011 - 03:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=235034&amp;s=09967a8bc4dd5c6b9bea7b523b098c49&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tsukeme

Reputation: 0
• Posts: 2
• Joined: 08-June 11

# 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
```#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);

scanf("%d", &inputNum2);

convertNumberIntoArray(inputNum1);

getchar();
getchar();
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: adding and subtracting large numbers using arrays

### #2 sk1v3r

Reputation: 231
• Posts: 668
• Joined: 06-December 10

## 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)
```   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 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: adding and subtracting large numbers using arrays

Posted 08 June 2011 - 02:28 PM

Are you talking about Arbitrary-precision 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:

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.

If you are not looking for arbitrary precision numbers please ignore everything I said here

### #4 tsukeme

Reputation: 0
• Posts: 2
• Joined: 08-June 11

## 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 post has been edited by tsukeme: 08 June 2011 - 02:46 PM

### #5 RetardedGenius

• ﻿>>──(Knee)──►

Reputation: 127
• Posts: 555
• Joined: 30-October 10

## Re: adding and subtracting large numbers using arrays

Posted 08 June 2011 - 03:32 PM

What you're trying to implement is called arbitrary precision arithmetic, coincidentally I'm currently writing my own APA library as a learning exercise. I would definitely recommend using object orientated design for this one. The easiest way to think of it is to apply the arithmetic algorithms you no doubt learned in school as a child. Good lucky mate!