Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Array
Home > Programs > Array

77. Write a program to merge two sorted array.

Answer:

#include <stdio.h> 
#include <iostream>
using namespace std;
void main()
{
    int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
    cout<<"\n Enter size of array Array 1: ";
    cin>>m;
    cout<<"\n Enter sorted elements of array 1: \n";
    for (i = 0; i < m; i++)
    {
        cin>>array1[i];
    }
    cout<<"\n Enter size of array 2: ";
    cin>>n;
    cout<<"\n Enter sorted elements of array 2: \n";
    for (i = 0; i < n; i++)
    {
        cin>>array2[i];
    }
    i = 0;
    j = 0;
    while (i < m && j < n)
    {
        if (array1[i] < array2[j])
        {
            array3[k] = array1[i];
            i++;
        }
        else
        {
            array3[k] = array2[j];
            j++;
        }
        k++;
    }
    if (i >= m)
    {
        while (j < n)
        {
            array3[k] = array2[j];
            j++;
            k++;
        }
    }
    if (j >= n)
    {
        while (i < m)
        {
            array3[k] = array1[i];
            i++;
            k++;
        }
    }
    cout<<"\n After merging: \n";
    for (i = 0; i < m + n; i++)
    {
        cout<<"\n"<<array3[i];
    }
}

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here: