Solved

Find common elements in two arrays

Posted on 2007-11-20
4
3,852 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
Comment Utility
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
Comment Utility
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
Comment Utility
How can we improve the code so it runs faster than NLogN ?
0
 
LVL 7

Expert Comment

by:lucky_james
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

772 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

15 Experts available now in Live!

Get 1:1 Help Now