sdsdsdsdas
sdsdsdsdas

Reputation: 171

Trying to find numbers repeated in two arrays

I am trying to find all of the numbers that are repeated across two arrays.. For example:

array1[2]:    1,2 

array2[2]:    1,5

The number that repeats itself is 1 so we create a new array that will contain 1.

array3[2]:    1

My code is:

int func1(int  *str, int *str2)
{
    int i,j,temp,c[10];
    for(i=0;i<*(str+i);i++){
        for(j=0;j<*(str2+j);j++){
            if(*(str+i) == *(str+j))
            {
                temp = *(str+i);
                *(str+i) = temp;
                temp = *(c+i);
                return c[i];
            }
        }
    }
    return 0;
}

What is the problem?(logically)

Thanks.

Upvotes: 0

Views: 223

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753665

There are multiple problems:

  1. The conditions in the two for loops are odd and probably wrong. They are equivalent to:

    for (i = 0; i < str1[i]; i++)
        for (j = 0; j < str2[j]; j++)
    
  2. You should probably specify the sizes of the input arrays in the function interface.

  3. In C, you must make sure you always know the sizes of the arrays.
  4. You should probably specify the output array in the function interface.
  5. Since you will need to know how many values were found in common, you'll need to return that number from the function.
  6. Your choice of the names str1 and str2 is unusual. Not technically wrong, but probably not a good idea. Such names should be reserved for character strings, not arrays of integers.
  7. Your local array c is barely used, and is not used safely.
  8. Your code returns when it finds the first pair of numbers that match, not all possible matches.
  9. The first two lines of the body of the if statement elaborately copies the value in str[i] back to itself via temp.
  10. The third line of the body of the if statement copies an uninitialized value from array c into the variable temp.
  11. The last line of the body of the if then returns that uninitialized value.

This adds up to changes such as:

int func1(int *arr1, int num1, int *arr2, int num2, int *arr3)
{
    int k = 0;
    for (int i = 0; i < num1; i++)
    {
        for (int j = 0; j < num2; j++)
        {
            if (arr1[i] == arr2[j])
                arr3[k++] = arr1[i];
        }
    }
    return k;
}

Note that this code assumes that the size of arr3 (the array, not the pointer itself) is as big as the product of num1 and num2. If both arrays contain a list of the same value, then there will be one row in the output array, arr3, for each pair so it could use num1 * num2 rows. This points out that the code does not deal with suppressing duplicates; if you need that (you likely do), then the body of the if statement needs to search through the current values in arr3 to check that the new value is not present. It would be wise to add another parameter, int siz3, to indicate the size of the third array; if you run out of space for values, you could then return -1 as an error indication.

The coded algorithm is quadratic (or, more accurately, proportional to the product num1 * num2). If you know the arrays are sorted on entry, you can reduce it to a linear algorithm (proportional to num1 + num2). With duplicate elimination, it is a little more expensive - it isn't quite as simple as 'cubic'. If you know the input arrays contain unique values (no duplicates), then duplicate elimination is obviously not necessary.

Upvotes: 3

Lucky Murari
Lucky Murari

Reputation: 12782

 for(i=0;i<*(str+i);i++){
        for(j=0;j<*(str2+j);j++){

Are wrong. You are applying '<' condition on an integer to itself and hence loop condition breaks. So, the loop never runs.

And why are you using these redundant statements?

 temp = *(str+i);
 *(str+i) = temp;

Also, this is wrong

temp = *(c+i);
return c[i];

Try more to correct those statements.If you can't do again, I will provide you a solution

Upvotes: 1

Related Questions