return a = 1 if list is a part of array else 0

I have stored values in array. Function 1 array stores the Id. Once this function stored all the value, I want to call this array to check whether a list is a part of this array or not. For example, list has elements/id {1,2,4} and array has elements/id {12,3,5,7,8}. how can I check whether the list a part of array?

int z[12];
int id=0;
int myid;

void MYArray(Node* node,......,int z[]){
............
...........
{
myid = nodeId;
id++;
}
z[id] = myid;
}


int CopmareArray(Node* node, int x[])
{
int a = 0,
for (list<Node*>::iterator itr = Nodes.begin(); itr != Nodes.end(); ++itr){

here I want to call the array and check whether If the above list is a part of array then return a = 1 else a = 0
Last edited on
> whether a list is a part of this array or not.
1
2
subset([], _). //an empty list is a subset
subset([X|Cdr], A):- member(X,A), subset(Cdr, A). //if the first element is in the array, and the rest of the list is a subset, then the list is a subset. 
Extremely sorry but could not get this. Would you mind to explain a little bit?


int CopmareArray(Node* node, int x[])
{
int a = 0,
for (list<Node*>::iterator itr = Nodes.begin(); itr != Nodes.end(); ++itr){
for (j=0; j<12; j++){ // I was running loop on my array like this but when I used cout<<x[j]<<endl; it printed "0" that means it is not taking array z[id] as argument
Last edited on
It's actually not so difficult.
Easiest if you create a function that checks if an elem is in the array.
Then you loop through all the elems in the list.
If one is not in the list, you can return 1.
At the end of the loop you return 0


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const int FOUND = 1;
const int NOT_FOUND = 0;
bool inArray(int needle, int[] haystack, size_t size)
{
   for ( int p =0; i < size; i++)
   {
      if (haystack[i] == needle)
        return true;
   }
   return false;
}

int CopmareArray(Node* node, int x[])
{
   for (list<Node*>::iterator itr = Nodes.begin(); itr != Nodes.end(); ++itr)
  {
     if(!inArray(itr, x, 12)
     {
         return NOT_FOUND;
     }
  }
  return FOUND;
} 
Last edited on
One could std::sort both the list and the array.
Then calculate (std::set_intersect) intersection of the list and array.
If the intersection is equal to the list, then the list elements are in the array.

All that with generic standard library algorithms and containers. Not the most efficient, but simple.


Do the list and array contain unique ID's? If not, then same ID can repeat in the array. That may need special treatment.

Are the list and array always sorted? If yes, that allows optimization.
If the sequences are sorted, std::includes http://en.cppreference.com/w/cpp/algorithm/includes

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> vec { 0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3 } ;
    std::list<int> lst { 0, 1, 1, 2, 3, 3 } ;
    std::cout << std::boolalpha
              << std::includes( std::begin(vec), std::end(vec), std::begin(lst), std::end(lst) ) << '\n' ; // true
}

http://coliru.stacked-crooked.com/a/1258b02c9883e4dd
Thank u so much everyone specially to @thomas and @JLBorges. I have done it in this way. void MYArray function is to store array which I has to pass int CopmareArray function.

The only issue I am facing is in cout statement "print array "<<x[j]<<endl; always shows me zero. That means my array is not been passed to this function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int CopmareArray(Node* node, int x[]); //Function Prototype

int z[12];
int id=0;
int myid;

void MYArray(Node* node,......,int []){
............
........... 
      {
         myid = nodeId;
          id++;
      }
      z[id] = myid;
}



Once above function has stored all the ids this is being called to check id and return either 0 0r 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int CopmareArray(Node* node, int x[])
{ 

 int a = 0,
 for (list<Node*>::iterator itr = Nodes.begin(); itr != Nodes.end(); ++itr){
    for (unsigned int j=0; j<sizeof(x)/sizeof(x[0]); j++){
	      if((*itr)->nodeId != x[j])
           cout<<"node "<<(*itr)->nodeId<<"print array "<<x[j]<<endl;
				   return a = 0;
			} 
		}
		
	    return a = 1;
	}
} 



Last edited on
closed account (48T7M4Gy)
For example, list has elements/id {1,2,4} and array has elements/id {12,3,5,7,8}. how can I check whether the list a part of array?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

int main()
{
    int list[] = {8, 2, 4, 333};
    int array[] = {1, 2, 333, 4, 5, 66, 7, 8};
    
    int list_size = sizeof(list)/sizeof(int);
    int array_size = sizeof(array)/sizeof(int);
    
    int *intersection = new int[array_size];
    for(int i = 0; i < array_size; i++)
    {
        intersection[i] = -99;
    }
    
    int temp = 0;
    for (int i = 0; i < list_size; i++)
    {
        temp = list[i];
        
        for (int j = 0; j < array_size; j++)
        {
            if ( temp == array[j] )
                intersection[i] = temp;
        }
    }
    
    for(int i = 0; i < array_size; i++)
    {
        if(intersection[i] > 0)
        {
            std::cout << intersection[i] << " intersects \n";
        }
    }
    
    return 0;
}
8 intersects 
2 intersects 
4 intersects 
333 intersects 
Program ended with exit code: 0
@kemort thank you so much nut my main issue is to is in cout statement "print array "<<x[j]<<endl; always shows me zero. That means my array is not been passed to my function.
closed account (48T7M4Gy)
@aisha I know but I thought out of interest more than anything I would go back to your original post and demonstrate how easy it is to do with simple arrays using keskiverto's apt terminology re intersection.

I will have a look at your (later) problem which I already noticed but I admit I avoided because of the Node* stuff.

Perhaps if you show all your code.
Last edited on
@kemort thank you so much :)
closed account (48T7M4Gy)
unsigned int j=0; j<sizeof(x)/sizeof(x[0]); j++)


I think that's where your problem is, you must pass the size of x[] in the function parameter list and not calculate it within the function.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>

int CompareArray(int, int*, int);

int main()
{
    int list[] = {8, 2, 4, 333};
    int array[] = {1, 2, 333, 4, 5, 66, 7, 8};
    
    int list_size = sizeof(list)/sizeof(int);
    int array_size = sizeof(array)/sizeof(int);
    
    int temp = 0;
    
    int a = 0;
    
    int *intersection = new int[array_size];
    for(int i = 0; i < array_size; i++)
    {
        intersection[i] = -99;
    }
    for (int i = 0; i < list_size; i++)
    {
        temp = list[i];
        
        for (int j = 0; j < array_size; j++)
        {
            if ( temp == array[j] )
                intersection[i] = temp;
        }
        
        a = CompareArray(temp, array, array_size); // <--
        
        if( a == 1)
        {
            std::cout << temp << " intersects type 3\n";
        }
    }
    
    for(int i = 0; i < array_size; i++)
    {
        if(intersection[i] > 0)
        {
            std::cout << intersection[i] << " intersects type 1\n";
        }
    }
    
    return 0;
}

int CompareArray(int number_sought , int* aArray, int size_of_array)
{
    for (int i = 0; i < size_of_array; i++)
    {
        if ( number_sought == aArray[i])
        {
            std::cout << number_sought << " intersects type 2\n";
            return 1;
        }
    }
    
    return 0;
}
8 intersects type 2
8 intersects type 3
2 intersects type 2
2 intersects type 3
4 intersects type 2
4 intersects type 3
333 intersects type 2
333 intersects type 3
8 intersects type 1
2 intersects type 1
4 intersects type 1
333 intersects type 1
Program ended with exit code: 0
Last edited on
if I use void MYArray(.......,int []) instead of int main() does it effect? bcz I already had a prog void MYArray(..............,int []) and array is just a part of it to store and pass Ids. I am trying to store values in array and passing it to the int CopmareArray(........., int x[]) . I have seen everywhere int main() is used to define and pass array while in my case it is void MYArray(..............,int []).
closed account (48T7M4Gy)
Forget about int main() because every program must have it and only once.

I changed from void back to int to show you 3 different ways you can use the function.

Topic archived. No new replies allowed.