CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    May 2009
    Posts
    2,413

    Re: Compacting an array in C++

    Quote Originally Posted by wael.salman View Post
    what do you mean by:

    First you write code that just iterates through the whole array and copies each array number to itself using the two indexes.????
    Like this,

    Code:
    int w=0; // write index
    int r=0; // read index
    while (r < array_size) {
       array[w++] = array[r]; // copy number, increment write index by 1
       r++; // increment read index by 1
    }
    The array and array_size are defined elsewhere.

    The above just copies the array on itself. You modify it so the r index is incremented in each iteration to skip a whole sequence of equal numbers. The w index is incremented as before. The effect is that only one number of any equal number sequence is copied.
    Last edited by nuzzle; October 23rd, 2009 at 07:23 AM.

  2. #17
    Join Date
    May 2009
    Posts
    2,413

    Re: Compacting an array in C++

    doublepost by mistake

  3. #18
    Join Date
    Jun 2009
    Posts
    89

    Re: Compacting an array in C++

    The above just copies the array on itself... Why we should do that??
    Can you give me full solution so I can understand.

  4. #19
    Join Date
    May 2009
    Posts
    2,413

    Re: Compacting an array in C++

    Quote Originally Posted by wael.salman View Post
    The above just copies the array on itself... Why we should do that??
    Can you give me full solution so I can understand.
    You often develop a solution in this way, incrementally in steps. You start with something simple and modify it to do something more complex.

    If you don't understand why the modification I've suggested leads to a solution to your problem then there's no way you'll ever understand a complete solution if I posted one.

    The only way for you to get something out of this is to start fiddling around with the code I posted and not stop until you arrive at a solution.

  5. #20
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Compacting an array in C++

    Quote Originally Posted by wael.salman View Post
    what do you mean by:

    First you write code that just iterates through the whole array and copies each array number to itself using the two indexes.????


    Thank you
    http://www.cplusplus.com/reference/algorithm/unique/

    If you don't want to use std::unique, then just use the algorithm.

  6. #21
    Join Date
    Jun 2009
    Posts
    89

    Re: Compacting an array in C++

    bool bRemoveDuplicates(int array[], int iSize){
    if(iSize <1)
    return false;
    if(array == NULL)
    return false;
    if(iSize == 1)
    return true;

    int left_comp = 0;
    int right_comp = 1;
    bool start_move = false;
    int hole = iSize;

    for(; right_comp <iSize;right_comp++,left_comp++){
    if(array[left_comp] == array[right_comp]){
    if(!start_move){
    start_move = true;
    hole = right_comp;
    }
    }
    else if(start_move){
    array[hole++] = array[right_comp];
    }
    }

    for(;hole<iSize;hole++){
    array[hole] = 0;
    }

    return true;
    }

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured