Solved

Find common elements in two arrays

Posted on 2007-11-20
4
3,887 Views
Last Modified: 2008-02-01
Hi..

I have the following program..

void FindCommonArrayElements(int a[], int sizeofa, int b[], int sizeofb)
{
   int c[10];
      for(int i=0;i<sizeofa;i++)
      {
            for(int j=0;j<sizeofb;j++)
            {
                  if(a[i] == b[j])
                  {
                        c[i] = a[i];
                  }
            }
      }
          for (int i = 0; i < sizeofa; i++)
           printf("c[%d] = %d\n", i, c[i]);
}

int main()
{
    int a[] = {1, 3, 5, 7, 9, 10, 11, 14, 15, 2};
      int b[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      int sizeofa = sizeof(a)/sizeof(int);
      int sizeofb = sizeof(b)/sizeof(int);

      FindCommonArrayElements(a, sizeofa, b, sizeofb);

}

The output is correct but has some uninitialized values.. How to fix the program to get rid of the garbled data in between?

c[0] = 1
c[1] = 3
c[2] = 5
c[3] = 7
c[4] = 9
c[5] = 10
c[6] = -858993460
c[7] = -858993460
c[8] = -858993460
c[9] = 2

0
Comment
Question by:niftyhawk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
lucky_james earned 125 total points
ID: 20326028
The problem is with the counters.
You can keep i for arr A
                      j for arr B
and can keep k for arr C


do  c[k] = a[i];
instead of  c[i] = a[i];

and then increment k after the above statement.


Hope it helps.
0
 
LVL 1

Author Comment

by:niftyhawk
ID: 20326039
ah..yes it works now..thanks :)

void FindCommonArrayElements(int a[], int sizeofa, int b[], int sizeofb)
{
   int c[10];
   int k=0;
      for(int i=0;i<sizeofa;i++)
      {
            for(int j=0;j<sizeofb;j++)
            {
                  if(a[i] == b[j])
                  {
                        c[k] = a[i];
                        k++;
                  }
            }
      }
          for (int i = 0; i < k; i++)
           printf("c[%d] = %d\n", i, c[i]);
}

int main()
{
    int a[] = {1, 3, 5, 7, 9, 10, 11, 14, 15, 2};
      int b[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      int sizeofa = sizeof(a)/sizeof(int);
      int sizeofb = sizeof(b)/sizeof(int);

      FindCommonArrayElements(a, sizeofa, b, sizeofb);

}
0
 
LVL 1

Author Comment

by:niftyhawk
ID: 20326048
How can we improve the code so it runs faster than NLogN ?
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20326087
your arrays are sorted. why dont you implement binary search in looking for the match?

this will prove much faster than current N^2 code complexity.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question