# finding common numbers

• 11-08-2009
BB89
finding common numbers
I am trying to find the numbers in the two arrays that are in common.

Say they are:

int A[3] = {1, 2, 5};
int B[4] = {1, 5, 9, 10};

Answer should say A \ B = {1, 5}

Code:

```#include <stdio.h> int main (void) {     int A[3] = {1, 2, 5};     int B[4] = {1, 5, 9, 10};     int x, z;   for( int x = 0; x < 3; x++ )     for( int z = 0; z < 4; z++ ) }```
I have declared the arrays and I am think the rest is correct.

I'm not sure how to find the common numbers or how to associate the loops with the arrays.

New to C and Arrays.
• 11-08-2009
RockyMarrone
What you have to do

Two arrays lets say A and B

Code:

```    for i = 0  to n - 1 of A         for j = 0 to n -1 of B           if value of A[i] == B[j] then add to answer array```
• 11-09-2009
BB89
Code:

```#include <stdio.h> int main (void) {     int A[3] = {1, 2, 5};     int B[4] = {1, 5, 9, 10};     int x, z;   for( int x = 0; x < 3; x++ )     for( int z = 0; z < 4; z++ )         if( A[x] == B[z] )             printf("A \ B = {%d, %d\n}", A[x], B[z] );     return 0; }```
My output: A \ B = {1,1}
A \ B = {5, 5}

The numbers are correct.

What's wrong.
• 11-09-2009
zalezog
When
A[x] == B[z]
then
Code:

`printf("A \ B = {%d, %d\n}", A[x], B[z] );`
A[x] and B[z] are same you need to print any one.

EDIT : You are declaring x and z twice.
• 11-09-2009
BB89
Ahh, makes since.

So

Code:

```  printf("A \ B = {%d}", A[x] );```
Why is it printing it twice?

It doing this, I dont understand it.

A \ B = {1,1}
A \ B = {5, 5}

Im wanting jusr A \ B = {1, 5}
• 11-10-2009
iMalc
Quote:

Originally Posted by RockyMarrone
What you have to do

Two arrays lets say A and B

Code:

```    for i = 0  to n - 1 of A         for j = 0 to n -1 of B           if value of A[i] == B[j] then add to answer array```

If A and B each held 10000 numbers or more, would you still use the same algorithm?

The arrays are sorted. If it's a safe assumption that they are always sorted then there's a much quicker approach. Try studying the C++ standard library code for set_intersection.

Can we also assume there are no duplicates?
• 11-10-2009
RockyMarrone
Quote:

Originally Posted by iMalc
If A and B each held 10000 numbers or more, would you still use the same algorithm?

The arrays are sorted. If it's a safe assumption that they are always sorted then there's a much quicker approach. Try studying the C++ standard library code for set_intersection.

Can we also assume there are no duplicates?

Ok iMac Please tell me if the numbers are unsorted then the algo
• 11-10-2009
laserlight
Quote:

Originally Posted by RockyMarrone
Ok iMac Please tell me if the numbers are unsorted then the algo

... would be to sort them and then use an algorithm that works when they are sorted.
• 11-10-2009
RockyMarrone
Quote:

Originally Posted by laserlight
... would be to sort them and then use an algorithm that works when they are sorted.

So how many compressions will be there less the one above of yours or more ????
And which will be faster
• 11-10-2009
laserlight
Quote:

Originally Posted by RockyMarrone
So how many compressions will be there less the one above of yours or more ????
And which will be faster

I think you mean "comparisons" instead of "compressions". Unfortunately, I do not really understand your sentence. What algorithms are you trying to compare?
• 11-10-2009
RockyMarrone
Quote:

Originally Posted by laserlight
I think you mean "comparisons" instead of "compressions". Unfortunately, I do not really understand your sentence. What algorithms are you trying to compare?

Then without knowing the algo how can u write

Quote:

Quote:
Originally Posted by RockyMarrone
Ok iMac Please tell me if the numbers are unsorted then the algo
... would be to sort them and then use an algorithm that works when they are sorted.
• 11-10-2009
Brain_Child
Quote:

Originally Posted by BB89
Ahh, makes since.

So

Code:

```  printf("A \ B = {%d}", A[x] );```
Why is it printing it twice?

It doing this, I dont understand it.

A \ B = {1,1}
A \ B = {5, 5}

Im wanting jusr A \ B = {1, 5}

you have your printf("a\b...}"); in the middle of a for loop. so everytime you find a matching pair you are printing the entire line. you need to take it out of the for loop and print it only once. you can either stick the statement before the for loops, or after the for loops.

also i cant see an easy way to print matching values inside nested for loops (due to the required format of the output, particularly the comma). you'll need to store the matching values somewhere and print them later, its about 5 extra lines.

(you should also fix up your indenting. either 3 spaces or 4 spaces. pick one and use it all the time)
• 11-10-2009
laserlight
Quote:

Originally Posted by RockyMarrone
Then without knowing the algo how can u write

Ah, but I do know the algorithm that iMalc was talking about ;)
• 11-10-2009
BB89
I figured it out.

Thanks for the help.
• 11-10-2009
laserlight
Quote:

Originally Posted by BB89
I figured it out.

Great. What did you use in the end?