Thread: Concatenate array elements more then specified range

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    Concatenate array elements more then specified range

    Hi
    I m concatenating specific range of digits that are same from an array "element" . I have problem with range.As in my case the range is col_elem[ii]=4, but it will continue reading the next element if it is the same. My code is:

    Code:
    int element[8]={0,1,3,3,3,0,1,2};
    col_elem[ii]=4;
    
            for (rr=0; rr<col_elem[ii];rr++){
              join_tmp[qq]=rr;
              while ((element[ii]== element[ii+1]) ) {
                join_tmp[qq]= concatenate(rr+1, join_tmp[qq]);
                printf("%d\n",join_tmp[qq]);
                rr++;
              }
              qq++;
            }
    Code:
    //FUNCTION TO CONCATENATE INTEGER VALUES OF SAME GROUP IN A COLUMN
    unsigned concatenate(unsigned x, unsigned y) {
      unsigned pow = 10;
      while(y >= pow)
        pow *= 10;
      return x * pow + y;        
    }

    Code:
    I want output like
    0
    1
    33
    instead i get
    0
    1
    333
    How can i fix it? Thanks

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I think you need to post more code. For example, where are rr, ii and qq defined? We can't see them here so we can't easily run your code to try it out. How is col_elem defined? When working with arrays you need to make sure you are indexing them appropriately, and without seeing these details its hard to verify just from what you posted.

    Also using `pow' as a local variable name is confusing because pow is the name of a standard function from the math library. I think it's legal the way that you wrote it, but it looks suspect. Its like trying to name a variable `printf' - you can legally do it but its a bad idea

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    element is an array of arbitrary length, but you only want to consider the first 4. So declare a variable N, and set it to 4. Now in the while, test that element[i] == element[i+Nrepeats], and also that + i Nrepeats <= N.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    qq,ii,rr are just rendom indexes to iterate. I just post the part that i think is useful and exclude them. But it original code i declare them.
    lets make int col_elem=4;
    instead of col_elem[ii]=4;

    Quote Originally Posted by c99tutorial View Post
    I think you need to post more code. For example, where are rr, ii and qq defined? We can't see them here so we can't easily run your code to try it out. How is col_elem defined? When working with arrays you need to make sure you are indexing them appropriately, and without seeing these details its hard to verify just from what you posted.

    Also using `pow' as a local variable name is confusing because pow is the name of a standard function from the math library. I think it's legal the way that you wrote it, but it looks suspect. Its like trying to name a variable `printf' - you can legally do it but its a bad idea
    Last edited by gevni; 04-19-2013 at 01:17 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Looks to me like you need to read this first:
    How To Ask Questions The Smart Way
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting a range of values and moving them. (array)
    By kuletako327 in forum C Programming
    Replies: 15
    Last Post: 10-27-2011, 02:51 AM
  2. Print numbers within a certain range of an Array
    By omegasli in forum C Programming
    Replies: 4
    Last Post: 04-10-2011, 01:08 AM
  3. Array (range lowest to highest)
    By flutegirl516 in forum C Programming
    Replies: 7
    Last Post: 02-18-2010, 10:20 AM
  4. Replies: 15
    Last Post: 07-24-2008, 03:03 AM
  5. maximum range for declaring a array of register type
    By anjana in forum C Programming
    Replies: 1
    Last Post: 05-24-2007, 05:42 AM