Reputation: 31
In this program, I am trying to find values that are common to two arrays and then put those common values into a third array called destination. However, I am putting values from both arrays into destination, when I only want values in source1 to go into destination. How do I fix this?
int common_elements(int length, int source1[length], int source2[length],
int destination[length]) {
int i = 0;
int k = 0;
while (i < length) {
int j = 0;
while (j < length) {
if (source1[i] == source2[j]) {
destination[k] = source1[i];
k++;
}
j++;
}
i++;
}
return k;
}
Upvotes: 3
Views: 122
Reputation: 123
Assuming that you want the destination array to have all the elements from source1
which are contained in source2
(irrespective of the order of occurrence), all you need to is to add a break;
statement after you increment k
so that you break out of the inner(j) loop and continue with the next iteration of the outer(i) loop.
If source1 = [1, 2, 3, 4, 5] and source2 = [1, 2, 3, 2, 1]
Your code will result in destination = [1, 1, 2, 2, 3]
If you add a break statement as suggested above, the code will result in destination = [1, 2, 3, (garbage value), (garbage value)].
To get rid of garbage values you can initialize the destination array with 0s or -1s before passing it to the method.
If you are interested in only the number of common elements, a break statement should be enough.
Edit:
Added the code:
int common_elements(int length, int source1[length], int source2[length],
int destination[length]) {
int i = 0;
int k = 0;
while (i < length) {
int j = 0;
while (j < length) {
if (source1[i] == source2[j]) {
destination[k] = source1[i];
k++;
break;
}
j++;
}
i++;
}
return k;
}
Upvotes: 2
Reputation: 359
I understand this problem like this:
you want to join elements of the same value in pairs, each element can only be in one pair, you want to copy value of each pair to destination array.
This will make sure values from source1 and source2 are used only once.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int
common_elements (int length, int source1[length], int source2[length],
int destination[length])
{
int i = 0;
int k = 0;
bool *used = malloc (sizeof (bool) * length);
while (i < length)
{
int j = 0;
while (j < length)
{
if (source1[i] == source2[j] && used[j] == false)
{
destination[k] = source1[i];
k++;
used[j] = true;
break;
}
j++;
}
i++;
}
free(used);
return k;
}
int
main ()
{
int destination[3];
int s1[] = {1,2,3};
int s2[] = {1,1,2};
int k = common_elements(3,s1,s2,destination);
printf ("%d", k);
return 0;
}
Upvotes: 0
Reputation: 9209
edit If you're after unique numbers that occur within both arrays.
Is this what you're after?
int common_elements(int length, int source1[length], int source2[length],
int destination[length]) {
int i = 0;
int k = 0;
while (i < length)
{
if (source1[i] == source2[i])
{
for (int j=0; j<k; j++)
{
if(destination[j] == source1[i])
continue;
}
destination[k] = source1[i];
k++;
}
i++;
}
return k;
}
BTW This wont handle initialised values in the array e.g. you will see 1,2,3,0,0 in destination.
Upvotes: 0