4 Replies - 10446 Views - Last Post: 08 June 2011 - 03:32 PM Rate Topic: -----

#1 tsukeme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);

    printf("please enter a number less than the first please: ");
    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  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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:

View PostNickDMax, 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 :)
Was This Post Helpful? 0
  • +
  • -

#4 tsukeme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • 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! :)

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... :stupid:

This post has been edited by RetardedGenius: 08 June 2011 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1