Combining int array elements to a single int variable

This is a discussion on Combining int array elements to a single int variable within the C Programming forums, part of the General Programming Boards category; Hi all, I'm an extreme novice (so please forgive me) and am trying to combine elements of an array into ...

  1. #1
    Registered User kpotato's Avatar
    Join Date
    Mar 2011
    Posts
    2

    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. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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;
    Last edited by itsme86; 03-01-2011 at 11:01 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int x;
    int total = 0;
    
    for (x = 0; x < 3; x++)
      total = (total * 10) + array[x];

  4. #4
    Registered User kpotato's Avatar
    Join Date
    Mar 2011
    Posts
    2
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM

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