Representing a Large Integer with an Array

This is a discussion on Representing a Large Integer with an Array within the C Programming forums, part of the General Programming Boards category; I'm a real beginner. I'm trying to create an array of integers to represent a very large number (three digits ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Representing a Large Integer with an Array

    I'm a real beginner. I'm trying to create an array of integers to represent a very large number (three digits of the number per element of the array). I'll start out by reading two large numbers as strings from a file, and converting each of them to an integer array. Then I need to perform addition, subtraction, and multiplication on these two arrays, and store the results in a third array. Then, I'll convert the resulting integer array back into a string and output it.

    The sizes of the arrays and the segment of code used to add the numbers (below) were given to me by my professor. He also gave us the basic idea for converting to/from a string/integer array, but I'm quite sure I haven't executed it correctly. Here's what I've got so far, just for the addition operation:

    Code:
    void main()
    {
        int i, ca, length1, length2, length3, t;
        char s1[20], s2[20], s3[20];   //s1, s2 are read from file, s3 will be printed result
        int a[16], b[16], c[16];  //used in performing arithmetic operations
        FILE *ff;
        
        ff = fopen("a.txt", "r");
        
        fscanf(ff, "%s", s1);  //1st large number
        fscanf(ff, "%s", s2);  //2nd large number
        length1 = strlen(s1);
        length2 = strlen(s2);
    
    //CONVERTS LARGE NUMBER STRINGS TO INT ARRAYS
        for(i = 0; i < 16; i++){
            a[i] = (s1[length1 - (3 * i + 3)] - 48)*100 + (s1[length1 - (3 * i + 2)] - 48)*10 + (s1[length1 - (3 * i + 1)] - 48);
            b[i] = (s2[length2 - (3 * i + 3)] - 48)*100 + (s2[length2 - (3 * i + 2)] - 48)*10 + (s2[length2 - (3 * i + 1)] - 48);
        }
    
    //TO ADD THE TWO ARRAYS
        for(i = 0; i < 22; i++){   //adds
            c[1] = a[i] + b[i];
        }
        for(i = 0; i < 21; i++){  //rectifies carry-over from addition
            ca = c[i] / 10;
            c[i] = c[i] % 10;
            c[i + 1] = c[i + 1] + ca;
        }
    
    //CHANGE RESULTING INT ARRAY INTO A STRING
        for(i = 0; i < 16; i++){
            s3[i] = c[3 * i] % 10 + 48;
            c[3 * i] = c[3 * i] / 10;
        }
    
        length3 = strlen(s3);
    
    //REVERSES STRING
        for(i = 0; i < length3 / 2; i++){
            t = s3[i];
            s3[i] = s3[length3 - i - 1];
            s3[length3 - i - 1] = t;
        }
    
    //PRINTS RESULT FROM ADDITION    
        printf("The sum is %s \n", s3);
        
        fclose(ff);
    }
    Does this look remotely close at all?

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Have you run your code? Its a lot easier to run and test it yourself, then if it has problems letting us know what that problem is. Otherwise people will likely just glance at your code for glaring errors *coughVOIDMAINcough* and probably miss what it is you're looking for. Plus, if it works perfectly, then you don't need our help at all.

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    Plus, unless this is just for fun, consider doing a google for 'bignum library', or download gmp from

    http://swox.com/gmp/

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,775
    Incidentally, have you considered that you're doing arithmetic with base 1000?
    I suspect that your carry over from addition code might not work, having been designed for base 10 instead of base 1000.

    You might also want to read more about Arbitrary-Precision Arithmetic.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Replies: 6
    Last Post: 04-12-2002, 08:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21