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?
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
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
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.
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 (unsignedint 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;
}
}
@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.
@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.
#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 = newint[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
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 []).