0% found this document useful (0 votes)
63 views

1.intersection and Union of Two Sets Using 1-D Array PDF

The document describes a C++ program that finds the intersection and union of two 1D arrays representing sets. The program takes input for the elements of each set and uses nested loops to compare elements and find any common elements for the intersection. All elements from both sets are added to the union array, omitting duplicates. The output prints the intersection and union.

Uploaded by

Arnab Dey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

1.intersection and Union of Two Sets Using 1-D Array PDF

The document describes a C++ program that finds the intersection and union of two 1D arrays representing sets. The program takes input for the elements of each set and uses nested loops to compare elements and find any common elements for the intersection. All elements from both sets are added to the union array, omitting duplicates. The output prints the intersection and union.

Uploaded by

Arnab Dey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab-Report-01

Intersection and union of two sets 1-D array


Objectives:
In this code, I firstly input the two sets represented as 1-D array set-1 and set-2. Then, I used nested
loops to find the intersection of the two sets by comparing each element of set-1 with each element
of set-2. If an element is found to be present in both sets, I add it to the intersection array. To find
the union of the two sets, we simply copy all the elements of set1 to union_set and then iterate
through each element, I have to check if it is already present in union_set. If it is not, we add it to
union_set.

Code:
#include <iostream>

using namespace std;

const int MAX_SIZE = 100;

int main() {

int set1[MAX_SIZE], set2[MAX_SIZE], intersection[MAX_SIZE], union_set[MAX_SIZE];

int n1, n2, intersection_size = 0, union_size = 0;

// Input set1

cout << "Enter the number of elements in set1: ";

cin >> n1;

cout << "Enter the elements of set1: ";

for (int i = 0; i < n1; i++) {

cin >> set1[i];

// Input set2

cout << "Enter the number of elements in set2: ";

cin >> n2;

cout << "Enter the elements of set2: ";

for (int i = 0; i < n2; i++) {

cin >> set2[i];

// Intersection
for (int i = 0; i < n1; i++) {

for (int j = 0; j < n2; j++) {

if (set1[i] == set2[j]) {

intersection[intersection_size++] = set1[i];

break;

// Union

for (int i = 0; i < n1; i++) {

union_set[union_size++] = set1[i];

for (int i = 0; i < n2; i++) {

bool found = false;

for (int j = 0; j < union_size; j++) {

if (set2[i] == union_set[j]) {

found = true;

break;

if (!found) {

union_set[union_size++] = set2[i];

// Output intersection

cout << "Intersection: ";

for (int i = 0; i < intersection_size; i++) {

cout << intersection[i] << " ";

cout << endl;

// Output union
cout << "Union: ";

for (int i = 0; i < union_size; i++) {

cout << union_set[i] << " ";

}cout << endl;

return 0;

Input:
Enter the number of elements in set1: 5
Enter the elements of set1: 1 3 5 7 9
Enter the number of elements in set2: 4
Enter the elements of set2: 2 4 6 8

Output:
Intersection: (Null)
Union: 1 3 5 7 9 2 4 6 8
Since the two sets have no elements in common, the intersection is an empty set. The union
contains all the elements from both sets.

You might also like