Skip to main content

Merge Arrays : Merge Two Arrays Of Different Size In C++

Merge Two Arrays C++ Program

C++ program to merge two arrays of different size. First array size is m and second array size is n. The size of the first array is greater than the first array i.e. m>n.
                             

Algorithm:-
  • Enter the size of first array m.
  • Enter the size of 2nd array n.
  • Enter the elements of 1st and 2nd array and number of elements of first array should be greater than the number of elements of second array.
  • After m index of first array, insert the element of second array into the first array up to m+n-1.
  • Sort the merge array of size m+n.
  • Display the first array, which will display all the elements of the merge array of size m+n.

Program:-

#include<iostream>
using namespace std;
int main()
{
int i,n,m,a[15],b[5],j;//array a[] and b[] are declared
cout<<"Enter the size 1st array:-"<<endl;
cin>>m;
cout<<"Enter the size of 2nd array(n<m):-"<<endl;
cin>>n;
cout<<"Enter the elements of 1st array, m elements:-"<<endl;
for(i=0;i<m;i++)
{
cin>>a[i];//enter the elements of the first array
}
cout<<"Enter the elements of 2nd array:-"<<endl;
for(i=0;i<n;i++)
{
cin>>b[i]; // enter the elements of second array
}
j=m; // j is initialized to m, the ending index of the first array.
for(i=0;i<n;i++)
{
   a[j]=b[i];
   j++; //incrementing the j from m to m+n-1 to insert the elements of second array
}
//sorting the m+n array or merge array
for(i=0;i<m+n;i++)
{
for(j=0;j<(m+n);j++)
{
   if(a[i]>a[j])
   {
    int temp=a[j];//int temp array is declared and it contained first element of 2nd array
    a[j]=a[i];
    a[i]=temp;
   }
}
}
int mid=(m+n)/2;
cout<<"Merge m+n array:-"<<endl;
for(i=m+n-1;i>=0;i--)
{
cout<<a[i]<<" ";
}

return 0;
}

Output:-
Enter the size 1st array:-
5
Enter the size 2nd array:-
8
Enter the elements of 1st array, m elements:-
6 8 9 2 12 52 34 78
Enter the elements of n array:-
34 67 90 62 70
Merge m+n array:-
2 6 8 9 12 34 34 52 62 67 70 78 90

Explanation:-

In this program, we have entered the size of both the array and entered the elements of both the array according to the size of the arrays. We have already declared the array with the size such as a[15] and b[5]. It means the maximum number of elements array a[] can hold is 15 and array b[] can hold 5. We can also change the size of array in the code or we can pass first size of array a[] and b[] as m and n respectively. After that we can declare the array such as a[m] and b[n]. 

Comments

Popular posts from this blog

Fix Breadcrumbs Error And m=1? In Blogger

  Remove Breadcrumbs Errors And ?m=1 Breadcrumb is a small path. It points your location on your site. It also affects the SEO of your website.  Use canonical URL , so: data:blog.url.canonical  instead of data:blog.url. It should work for both desktop and mobile versions. It means the same URL works on both mobile and desktop without producing the ?m=1 or ?m=0. Before this, I am going to tell you what all the portions I will explain and fix your problem. First we will fix Breadcrumbs Error . This is the most dangerous for your website. Second we will remove ?m=1 and ?m=0, tags from our mobile view URL. Remove Breadcrumbs Error :- You should follow the below steps, so that you would fixed this error :- Go to blogger.com Login to blogger with your blogger email id Click on Template Click edit HTML, you have to edit HTML theme code. Press Ctr + F to open a search bar in your HTML theme code. Search for <div class='breadcrumbs' Paste the below code up to </b:loop> ...

API : What is an Application Programming Interface

Application Programming Interface API का पूरा नाम "Application Programming Interface" हैं| यह एक software intermediary है जो दो software applications को एक दूसरे से बात करने का कम करता हैं| जैसे कि उद्धरण के लिए Instagram पर आपको message आता है और आप अपने phone पर check कर लेते है API के कारण, क्योंकि API एक माध्यम है आपके फ़ोन में आप किसी भी application से इनफार्मेशन प्राप्त कर पते  हैं| जब आप अपने phone में  कोई  application use करते है तो application internet से जुड़ता है और data को server पर  भेजता है| तब वह server, data को लेता है और translate करता है इस data को , उसके बाद उस data पर जरुरी कार्य करता है और अंतिम में जो translated data है उसे phone पर send करता हैं| तब Application उस data को translate करता है और आपके फ़ोन पर दिखता है readable form में जैसा आप देखना चाहते हैं|  ये जितने भी कार्य है, phone से data को लेना और उसे translate कर के server को send करना और फिर उसे phone पर user के readable form में change करके उसके फ़ोन पर डिस्प्ले करना, ये सब API के मा...

Blogger : How To Create Blog Through Blogger & Its Overview

Overview Of Creation Of Blog Now days online learning and earning is very popular. Most of the people is trying to either learn or earn from their home. If you are a writer or story teller, you have interest in writing or reading new thing. So, This is very helpful for you, in this post i will tell you how to create your own blog website and earn money. Step follow to create a blog on blogger:- First you should have a email account and phone number to open or create a blogging account. You have to go to blogger website page, URL of that page is given below, simply you go and click on the URL. Click on the create new blog, after clicking, you have enter the blog name that you want to show to the user and you must choose or set the appropriate your own blog URL. After this process, you will have created your blog. You can handle everything post, pages, layout, inside the setting,you can set the user information and the account information. In the layout you can set layout from the given ...