Question

write a c++ program that merges two arrays a and b. You may use the following...

write a c++ program that merges two arrays a and b. You may use the following array contents A=5,7,8,6,3,3 and B=4,5,6,1,2,3.The merged array should exclude all repeating numbers

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

#include<bits/stdc++.h>
using namespace std;

int main(){
  
// initialize both the arrays
int a[]={5,7,8,6,3,3};
int b[]={4,5,6,1,2,3};
  
// n is sizeof array a , m is sizeof array both
// c is the final merged array of size m+n
int n=6,m=6,k=0;
int i=n-1,j=m-1;
int c[m+n];
  
// use map to mark to mark element as visited
map<int,int>mp;
  
for(int i=0;i<n;i++){
mp[a[i]]=0;
  
}
  
for(int i=0;i<m;i++){
mp[b[i]]=0;
  
}
  
// merge both the arrays
while(i>0&&j>0){
if(mp[a[i]]!=1){
c[k++]=a[i];
mp[a[i]]=1;
}
i--;
  
if(mp[b[j]]!=1){
c[k++]=b[j];
mp[b[j]]=1;
}
j--;
}
  
// if some element remains in array a
  
while(i>=0){
if(mp[a[i]]!=1){
c[k++]=a[i];
mp[b[i]]=1;
}
i--;
}
  
// if some element remains in array b
while(j>=0){
if(mp[b[j]]!=1){
c[k++]=b[j];
mp[b[j]]=1;
}
j--;
}
  
// print final array
cout<<"Final array after merging with duplicates removed is : \n";
  
for(int i=0;i<k;i++){
cout<<c[i]<<" ";
}
  
}

screenshots:

Output:

Add a comment
Know the answer?
Add Answer to:
write a c++ program that merges two arrays a and b. You may use the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • Write a method that takes 3 double arrays, merges all 3 arrays and returns a new...

    Write a method that takes 3 double arrays, merges all 3 arrays and returns a new array which stores all the values of the 3 arrays. Please use Java and comment the code thank you

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Arrays and reading from a file USE C++ to write a program that will read a...

    Arrays and reading from a file USE C++ to write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order. The program should only use one array and that array should not be changed after the numbers have been read. You should assume that the file does not contain more than 100 floating point numbers. The file name...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • 29. (20 points) Write the complete C++ program that implements the following program, as if you...

    29. (20 points) Write the complete C++ program that implements the following program, as if you were turning this in for homework. Your program should have a commented header (your name, etc), comments within the program and a well-built display for the output. Don't forget the pre-processor commands Define an array called nums with a maximum of 20 integers, and fill the array with numbers, recei the keyboard (prompt for the numbers.) In this program, exactly 20 integers will be...

  • C++ Write a program with the following elements: in main() -opens the 2 files provided for...

    C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...

  • c++ help Write a program that: Creates two finite arrays of size 10 These arrays will...

    c++ help Write a program that: Creates two finite arrays of size 10 These arrays will serve as a storing mechanism for student grades in Classes A and B Uses a loop to populate the arrays with randomly generated numbers from 0 to 100.These numbers will represent student grades. Compute average grade of each class. Finally, output the two arrays and the message with two averages and best class. ("Class A average is: 65.43; class B average is: 68.90. Class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT