# Thread: Combining int array elements to a single int variable

1. ## Combining int array elements to a single int variable

Hi all,

I'm an extreme novice (so please forgive me) and am trying to combine elements of an array into a single int variable. I have an array that is composed of numbers (0-3) and so would read something like:

032011320203010

I want to be able to take a group of three of those values and combine it into one. So I want to take the first three elements (0, 3, 2) and make it an int value 32. Or take the second set of three (0, 1, 1) and make it an int value 11.

I thought I could copy the three values into a character array and then use atoi to convert it into a single int, but it doesn't seem to be working (I get an int value of 0 instead of 32 for the first set).

This is what I have so far:

Code:
```    int sequence[3*LENGTH];
int residue, i, j, nucleotide;
char codon[3];
int realcodon;

getnucleotidesequence(sequence);

nucleotide = 0;

i = nucleotide;
for (j=0; j<3; j++)
{
codon[j] = (char)sequence[i];
++i;
}

realcodon = atoi(codon);```

Any help would be great appreciated!

Thanks so much,

Kayla

2. That could work. A couple of things:

1) atoi() works on strings, so make sure you create a string out of codon. That means allocating space for 4 chars instead of 3 to make room for the terminating '\0'.

2) To turn the int value 0 into the character '0' just add '0' to it. This works for any single digit int value. The digit chars are all sequential in the ASCII table so adding '0' to a single digit int value works (4 + '0' = '4', etc.). So you need codon[j] = (char)sequence[i] + '0'

Another way would be to just use a multiplier on a running sum:
Code:
```int sum = 0;
int multiplier = 100;
int i;
for(i = nucleotide;i - nucleotide < 3;++i, multiplier /= 10)
sum += sequence[i] * multiplier;```

3. Code:
```int x;
int total = 0;

for (x = 0; x < 3; x++)
total = (total * 10) + array[x];```

4. Oh wow, I didn't even think of doing a running sum... That would be much much easier!

I will add that in, thank you so much for your help!