This is the code ive got so far

``````#include <iostream>

int main()
{
int a[10] = {0};		//sets an array of ten zeros

int x;				//intialises x
int y;				//intialises y

std::cin>> x;			//reads in x input
std::cin>> y;			//reads in y input

if ( -1 < x && x < 10 )	//intialises from zero to 10

a[x] = 1;				//puts input of x to 1
a[y] = 1;				//puts input of y to 1

for ( int i = 0; i < 10; i++ )	//does a loop of zero's

std::cout<< a[i] <<' ';			//put inputs a[x] a[y] into loop
std::cout<<'\n';

int b[10] = {0};

int x1;

std::cin>> x1;			//reads in x1 input

if ( -1 < x && x < 10 )	//intialises from zero to 10

b[x1] = 1;

for ( int p = 0; p< 10; p++ )	//does a loop of zero's
std::cout<<b[p] <<' ';
std::cout<<'\n';

return 0 ;

}``````

it output two lines of code eg
00010101
01000000

i want to be able to create a third set that intersects the two previous sets and displays the output.
01010101

Thanks.

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by Lerner

I'm sure there's a more elegant solution but you could create an array where each element is the result of an OR result of the other two arrays.

``````for (int i=0; i<10; i++) {
array[i] = a[i] || b[i];
}``````

//intialises x
//intialises y

Nope those are declarations. The value of x and y at this point is random junk. This would be a declaration and initialization:

int x = 0;
_________________________

//intialises from zero to 10

Nope. That statement validates that x is has value of 0 to 9 inclusively such that x will be a valid index for array a. No such check is done for y or x1 however.
_____________________________

//does a loop of zero's

Nope, one of them creates a loop that outputs all values in a, the other all values in b.
____________________________________

To me, this array of integers could represent a bit pattern:

01010101

which is the result of the bitwise OR operator on these two:

00010101
01000000

but I wouldn't consider it the intersection of two int arrays. To me the intersection would be

0 0 0 0

which amounts an array of ints containing all elements a that have the same value of b, but I'll admit I've never thought much about it before or seen it discussed. If there is such a definition, I'm not aware of it, and if it's truly not defined I suppose you could define the intersection of two arrays to be anything you want.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.