Solved

Find common elements in two arrays

Posted on 2007-11-20
4
3,865 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now