|
-
October 23rd, 2009, 07:17 AM
#16
Re: Compacting an array in C++
 Originally Posted by wael.salman
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.
-
October 23rd, 2009, 07:22 AM
#17
Re: Compacting an array in C++
-
October 23rd, 2009, 08:00 AM
#18
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.
-
October 23rd, 2009, 08:31 AM
#19
Re: Compacting an array in C++
 Originally Posted by wael.salman
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.
-
October 23rd, 2009, 11:01 AM
#20
Re: Compacting an array in C++
 Originally Posted by wael.salman
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.
-
February 7th, 2010, 11:54 AM
#21
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;
}
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|