Solved

Find common elements in two arrays

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

820 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