Tuesday, 17 January 2012

Write a C++ program that will merge the contents of two sorted (ascending order) arrays of type double values by storing the result in a third array (still in ascending order). Both of the input arrays can be of same or different lengths declare arrays with fixed size of 50 and then asks user to specify the...

Write a C++ program that will merge the contents of two sorted (ascending order) arrays of type double values by storing the result in a third array (still in ascending order). Both of the input arrays can be of same or different lengths declare arrays with fixed size of 50 and then asks user to specify the number values he wish to enter for each arrays individually. The resultant array should not contains duplicate values, so while merging contents in the third arrays you have to take care that if a value exist in both input arrays then only its one copy should be placed in the resultant array.







#include <iostream>

#include <conio.h>

using namespace std;



int main()

{

   int m,n;

   cout << "\n Enter the Number of Elements in First Array(MAx 50) = ";

   cin >> m;

   cout << "\n Enter the Number of Elements in Second Array(MAx 50) = ";

   cin >> n;



    double a[50],b[50];



    cout << "\nEnter " << m << " Numbers in First Array \n";

    for(int e=0; e<m; e++)

    {     cout << e+1 << ": ";

            cin >> a[e];    }

      

//      Using Bubble Sort Method

   

     double loc1;



    for(int o=0;o<m-1;o++)     //Sorting of First Array in Ascending Order

    {    for(int p=0;p<m-1;p++)

            if(a[p]>a[p+1])

            {    loc1=a[p];

a[p]=a[p+1];

a[p+1]=loc1;    }

        }



    cout << "\nEnter " << n << " Numbers in Second Array \n";

    for(int f=0;f<n;f++)

    {       cout << f+1 << ": ";

            cin >> b[f];    }



    double loc2;

        for(int q=0;q<n-1;q++)     //Sorting of Second Array in Ascending Order

        {    for(int r=0;r<n-1;r++)

                        if(b[r]>b[r+1])

                {    loc2=b[r];

                    b[r]=b[r+1];

                    b[r+1]=loc2;    }

        }



    int l=m+n;            //Combining First and Second Array Length

    double c[l];



//Embedding First and Second Array in a Third Array

  

     for(int g=0;g<m;g++)

        c[g] = a[g];      

    for(int h=0;h<n;h++)

        c[m+h] = b[h];



    double loc3;



    for(int i=0;i<(l-1);i++)

    {    //Sorting of Third (Embedded) Array in Ascending Order

    for(int j=0;j<(l-1);j++)           

        if(c[j]>c[j+1])

        {    loc3=c[j];

            c[j]=c[j+1];

            c[j+1]=loc3;    }

        }

    double e[l];

    int r=0,p=0;

    for(int k=0; k<l; k++)

      {

        if(c[k+1]!=c[k])        //Removing Duplicates

            {e[r]=c[k];         //Embedding Third Array in Fourth

                r++;}

        else

            p=l-1;

      }

        l=l-(p-r+1);

//Printing Out the Final Sorted Array discarding Duplicate Values

        cout << endl << "\tThe Sorted Array is \n";

    for(int q=0;q<l;q++)

        cout << e[q] << endl;

    getche();

    return 0;

}



No comments:

Post a Comment