Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to find common element of two arrays storing the result into 3rd array and want to output third array.

What I have tried:

#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;

int main()
{

    int sizeofarray=0,i=0 ,j=0, num=0, answers[]={};
    cout<<"enter the size of array"<<endl;
    cin>>sizeofarray;//take size of array from user
    int array1[sizeofarray];
    int array2[sizeofarray];
    cout<<"please enter a sorted array member of Array1"<<endl;

    //input of array element
    for ( i=0 ; i<=sizeofarray; i++)
    {
        cin>>array1[i];
    }
    system("CLS");
    cout<<"please enter a sorted array member of Array2"<<endl;
    for (j=0 ; j<=sizeofarray; j++)
    {
        cin>>array2[j];
    }
    system("CLS");

    //comparing the array element and storing the similar items to another array
    while(array1[i]!=NULL && array2[j]!=NULL)
    {
        if(array1[i]==array2[j])
        {
            answer[num++]=array1[i];
            i++;
            j++;
        }
        else if(array1[i]<array2[j])
        {
            i++;
        }else{
            j++;
        }
        i++;
        j++;
    }
    cout<<"The number of common elements"<<num<<endl;
    cout<<"These are the common numbers: ";
    for (int k=0;k<num;k++)
    {
        cout<<answer[k]<<" ";
    }
    getch();
    return 0;
}
Posted
Updated 5-Mar-16 23:09pm
v3
Comments
Richard MacCutchan 6-Mar-16 3:12am    
And what is the problem?

1 solution

Note the C++ does not support constructs like
C++
cin>>sizeofarray;//take size of array from user
int array1[sizeofarray];

C++ native arrays need to have a size that is known at compile time, so the compiler can allocate memory for it at compile time. If you want to have an array that you can re-size at runtime you should use std::vector<>. For example:
C++
    cin >> sizeofarray;
    std::vector<int> array1 (sizeofarray);
</int>

The same applies to your arrays array2 and answer.

The next error is that you don't reset i and j before entering the while loop:
C++
while(array1[i]!=NULL && array2[j]!=NULL)

Hence, i and j will still have the value of sizeofarray+1 and you will be accessing elements that are out of bounds.

The condition in that while is the next problem. It looks like you are trying to test whether array elements contain a null pointer. But your array contain simple int values. Hence the comparison with NULL does not make sense. If you want to test whether i or j have left the upper array boundary you should compare them with sizeofarray.

Finally, the last i++ and j++ don't appear right to me. Run your program in a debugger and you will see what I mean.
 
Share this answer
 
Comments
CPallini 6-Mar-16 5:54am    
5.
Arthur V. Ratz 20-Mar-16 11:12am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900