Question

In: Computer Science

Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...

Overlapping Arrays (C++)

An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array overlaps the second.

Solutions

Expert Solution

Program

#include <iostream>
using namespace std;

int isOverlap(int array1[], int array2[],int n1, int n2) ;
int main()
{
int n1,n2,i;
//Read the size of first and second array
cout<<"Enter size of first array: ";
cin>>n1;
cout<<"Enter size of second array: ";
cin>>n2;
int array1[n1],array2[n2];
//Read the elements of first and second array
cout<<"Enter first array: ";
for(i=0;i<n1;i++)
cin>>array1[i];
cout<<"Enter second array: ";
for(i=0;i<n2;i++)
cin>>array2[i];


//Call the function to check if first array overlaps second array
if(isOverlap(array1,array2,n1,n2))
cout<<"First array overlaps the second array."<<endl;
else
cout<<"First array does not overlaps the second array."<<endl;
  

}
int isOverlap(int array1[], int array2[],int n1, int n2)
{
int i,j;
for (i = 0; i <n2; i++)
{
for (j = 0; j <n1; j++)
{
if(array2[i] == array1[j]) // Element found
break;
}
  
if (j == n1) //Second array not found in array1
return 0;
}
  
// All elements of array2 are present in array1
return 1;
}

Output

Enter size of first array: 5
Enter size of second array: 4
Enter first array: 1
7
3
4
2
Enter second array: 1
1
2
3
First array overlaps the second array.

Output 2
Enter size of first array: 4
Enter size of second array: 3
Enter first array: 1
6
7
3
Enter second array: 2
3
1
First array does not overlaps the second array.


Related Solutions

In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
This is C++ programing Reversing the elements of an array involves swapping the corresponding elements of...
This is C++ programing Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.Given an array a, an int variable n containing the number of elements in a, and two other intvariables, k and temp, write a loop that reverses the elements of the array.Do not use any other variables besides...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
Counting SortShow the B and C arrays after Counting Sort finishes on the array A [19,...
Counting SortShow the B and C arrays after Counting Sort finishes on the array A [19, 6, 10, 7, 16, 17, 13, 14, 12, 9] if the input range is 0-19.
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and a3 of type float as parameters. It then multiplies a1 and a2 and stored the result in a3. Assume that array multiplication is done by multiplying corresponding array elements, e.g. a3[i] = a1[i] * a2[i] where 0 <= i <= (array size – 1) Write a C++ main program to dynamically create three equal sized float arrays a1, a2 and a3. The size of...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that are the sum of two other elements. Use a selection sort algorithm, discussed in this chapter to sort the array. Instructions and code for Programming Exercise 14 have been included for your convenience. Exercise 14 Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT