Thread: Representing a Large Integer with an Array

  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
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    First off, this should be moved to the C forum instead of the C++ forum.

    Oh, and welcome to the boards.

    Hmm....it seems like the code you're using to concatenate numbers is kinda inefficient-ish, eh? The way you're "converting" the strings into array elements seems that way, at least.
    Last edited by Krak; 03-02-2005 at 10:23 PM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    Thanks lol.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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