Array Copy Help please I'm Confuse

1
2
int firstarray[10] = {1,2,3,4,5,1,2,3,4,5};
int second array[10];

i want to copy the first array into the second array but don't want to copy the duplicate values what algorithm is required i'm confuse please help me
closed account (Dy7SLyTq)
i would write your own
what??
closed account (Dy7SLyTq)
write your own algorithim. write a copy algorithim and then check each cell against the whole... wait why arent you using vectors?
I didn't study vectors yet i want to copy the array in second array duplicate values are not allowed and in the above code the second array must have 5 values and thy must b store at the first 5 locations of the second array
@DTS code can u help or i have to try by my self
Last edited on
The task can be simply done by means of std::accumulate.
I don't know about those function if you can help me with loops please
Or if you did not deal with standard algorithm std::accumulate you can write simply


1
2
3
4
5
6
7
8
9
10
11
12
int firstarray[10] = {1,2,3,4,5,1,2,3,4,5};
int secondarray[10];

int *p = secondarray;

for ( int x : firstarray )
{
   if ( std::find( secondarray, p, x ) == p ) *p++ = x;
}

for ( auto it = secondarray; it != p; ++it ) std::cout << *it << ' ';
std::cout << std::endl;
Last edited on
at line number 6 i'm getting a syntax error
this rang based for require suitable begin
Or with "pure" loops

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const size_t N = 10;
int firstarray[N] = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
int secondarray[N];

size_t n = 0;
for ( size_t i = 0; i < N; i++ )
{
   size_t j = 0;

   while ( j != n && secondarray[j] != firstarray[i] ) j++;

   if ( j == n ) secondarray[n++] = firstarray[i];
}

for ( size_t j = 0; j != n; j++ ) std::cout << secondarray[j] << ' ';
std::cout << std::endl;
@Sarmadas

at line number 6 i'm getting a syntax error
this rang based for require suitable begin


Line 6 is correct. Either you made a typo or you are using MS VS 2010. In the last case you can substitute the loop for

for each ( int x in firstarray )
Thanks
Topic archived. No new replies allowed.