-2

Can anyone please tell why I am not getting correct solution.. started programming few days ago...

There are 3 arrays and code works if there are less elements.. idk whats problem..please give me solution thanks..

here is my code:-

int arr1[]={1,2,3,5,7,8,9,11,12,18};
int arr2[]={2,3,4,12};
int arr3[]={2,3,5,12,19};
int arr4[10];
int k=0;
int x=0,y=0,z=0;

while(arr3[z]!=19)
{
    if(arr1[x]==arr2[y] && arr2[y]==arr3[z])
    {
        arr4[k]=arr1[x];
        cout<<" intersecting element found "<<arr4[k];
        k++;
        x++;y++;z++;
    }
    else if(arr1[x]<arr2[y])
    {
        x++;
    }
    else if(arr2[y]<arr2[z])
    {
        y++;
    }
    else
    {
        z++;
    }

}
for(int i=0;i<k;i++)
{
    cout<<endl<<" o/p "<<arr4[i];
}

for the given arrays its working perfect and showing me 2, 3 and 12 but if I add 5 in array2 it shows intersecting elements as 2 and 3 only ...whereas o/p should be 2,3,5,12

Ruks
  • 3,886
  • 1
  • 10
  • 22
Akash
  • 13
  • 3
  • What are you getting as a solution for the given arrays? Have you tried debugging you code to see what happens in each iteration? – o_weisman Jul 08 '18 at 06:39
  • Required: an [mcve]. When you post incomplete code a potential answer will have to fill in the blanks. Since they generally know what they are doing, the blanks they fill in may fix the bug you're looking for without them really even thinking about it. They see no error, they report no fix and move on. Recommendation: Step through the program [with a debugger](https://en.wikipedia.org/wiki/Debugger), line by line if you have to, and keep an eye out for where the program does something you do not expect. You will likely never find a better productivity tool than a debugger. – user4581301 Jul 08 '18 at 06:41
  • bro for the given arrays its working perfect and showing me 2, 3 and 12 but if I add 5 in array2 it shows intersecting elements as 2 and 3 only ...whereas o/p should be 2,3,5,12 – Akash Jul 08 '18 at 06:42
  • I don't think that should happen. Are you sure you just added 5 after 4 in arr2? – o_weisman Jul 08 '18 at 06:53
  • yes bro..can u please check :- http://prntscr.com/k3v36c – Akash Jul 08 '18 at 06:57
  • Is `else if(arr2[y] – Ruks Jul 08 '18 at 07:08
  • oh man thanks, a lot. Made a silly mistake... now working correctly... – Akash Jul 08 '18 at 07:23
  • I want to ask one more thing.. if u see in code while condition... I used arr3[z] ... can anyone tell any other solution for running while loop in this question... – Akash Jul 08 '18 at 07:25
  • @Akash I gave a similar answer below if you would like to see. Also, it is highly recommended to use vectors instead if you are using C++. A vector, similarly will look like this `std::vector arr1{1,2,3,5,7,8,9,11,12,18}`, etc. and you can also get its size using `arr1.size()` which is pretty useful and saves a lot of effort. – Ruks Jul 08 '18 at 07:48
  • @JeJo I have no idea what this vector is .. and what u guys using "auto" in your code... will read about these – Akash Jul 08 '18 at 09:13

1 Answers1

1

You can do this by looping through all the arrays at once inside one another's 'for' loop.

Example:-

int arr1[] = { 1,2,3,5,7,8,9,11,12,18 };
int arr2[] = { 2,3,4,12,5 };
int arr3[] = { 2,3,5,12,19 };
int arr4[10];
int index = 0;
for (auto i = 0; i < GetArraySize(arr1); i++)
    for (auto j = 0; j < GetArraySize(arr2); j++)
        for (auto k = 0; k < GetArraySize(arr3); k++)
            if (arr1[i] == arr2[j] && arr2[j] == arr3[k])
            {
                arr4[index] = arr1[i];
                index++;
                cout << arr1[i] << " is an intersecting element!" << endl;
            }
for (auto i = 0; i < index; i++)
    cout << " o/p " << arr4[i] << endl;

And put this line outside your main function:-

#define GetArraySize(Array) signed(sizeof Array / sizeof Array[0])

This line gives us the size of the array, which is explained here: How do I find the length of an array?

If your compiler supports C++11, you can just use range-based 'for' loop, which is much easier to read than this normal loop.

If you were to use range-based 'for' loop, it would look like:-

int arr1[] = { 1,2,3,5,7,8,9,11,12,18 };
int arr2[] = { 2,3,4,12,5 };
int arr3[] = { 2,3,5,12,19 };
int arr4[10];
int index = 0;
for (auto &elem : arr1)
    for (auto &elem2 : arr2)
        for (auto &elem3 : arr3)
            if (elem == elem2 && elem2 == elem3)
            {
                arr4[index] = elem;
                index++;
                cout << elem << " is an intersecting element!" << endl;
            }
for (auto i = 0; i < index; i++)
    cout << " o/p " << arr4[i] << endl;

Which is clearly easier to read.

Ruks
  • 3,886
  • 1
  • 10
  • 22