# Thread: Representing a Large Integer with an Array

1. ## 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);
}

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;
}

printf("The sum is %s \n", s3);

fclose(ff);
}```
Does this look remotely close at all?

2. 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. Plus, unless this is just for fun, consider doing a google for 'bignum library', or download gmp from

http://swox.com/gmp/

4. 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.