RSS

Category Archives: Merging of Arrays

MERGING OF ARRAYS USING POINTERS (WITHOUT CREATING FUNCTION)

// THIS PROGRAM MERGES TWO ARRAYS (USING POINTERS)

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int *arr1,*arr2,num1,num2;
cout<<“\nEnter the number of elements in first array\n”;
cin>>num1;
cout<<“\nEnter the number of elements in the second array\n”;
cin>>num2;
cout<<“\nInput first array\n”;

for(int i = 0; i<num1; i++)
{
cin>>*(arr1 + i);
}

cout<<“\nNow input second array\n”;

for(i = 0; i<num2; i++)
cin>>*(arr2+i);

cout<<“\nMerging the arrays\n”;
int *res;

for(i = 0; i<num1+num2; i++)
{
if(i<num1)
{
*(res + i) = *arr1;
arr1++;
}
else
{
*(res + i) = *arr2;
arr2++;
}
}

cout<<“\nThe merged arrays is \n”;

for(i = 0; i<num1+num2; i++)
cout<<*res++<<‘ ‘;

getch();
}

 
 

MERGING OF ARRAYS 3

/* THIS PROGRAM MERGES TWO ARRAYS ACC. TO SOME GIVEN ORDER

ARRAY 1: 3 4 5 6
ARRAY 2: 8 7 4 9
RESULT : 8 7 4 9 3 4 5 6
*/

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int arr1[100],arr2[100], res[200],size1 = -1,size2 = -1;

cout<<“\nEnter the size of the first array\n”;
cin>>size1;
cout<<“\nEnter the size of the second array\n”;
cin>>size2;
cout<<“\nInput first array\n”;

for(int i = 0; i<size1; i++)
{
cin>>arr1[i];
}

cout<<“\nInput second array\n”;

for(i = 0; i<size2; i++)
{
cin>>arr2[i];
}       // 8749 3456

for(i= 0; i<size2; i++)
res[i] = arr2[i];

for(int j =0; j<size1; j++,i++)
res[i] = arr1[j];

cout<<“\nThe merged array is \n”;

for(i = 0; i<size1 + size2; i++)
cout<<res[i]<<‘ ‘;

getch();
}

 
 

MERGING OF ARRAYS 2

/*
PROGRAM TO MERGE ARRAYS ACCORDING TO A GIVEN ORDER

ARRAY 1: 3 4 5 6
ARRAY 2: 8 7 4 9
RESULT : 6 5 4 3 9 4 7 8
*/

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int arr1[100],arr2[100],res[100],size1 = -1,size2 = -1;

cout<<“\nEnter the size of the first array(max 100)\n”;
cin>>size1;
cout<<“\nEnter the size of the second array (max 100)\n”;
cin>>size2;
cout<<“\nInput first array\n”;

for(int i = 0; i<size1; i++)
cin>>arr1[i];

cout<<“\nInput second array\n”;

for(i = 0; i<size2; i++)
cin>>arr2[i];

for(i = 0; i<size1; i++)
res[i] = arr1[size1 – 1 – i];

for(int j = 0; j<size2; j++,i++)
res[i] = arr2[size2 – 1 – j];

cout<<“\nThe merged array is \n”;
for(i = 0; i<size1+size2; i++)
cout<<res[i]<<‘ ‘;

getch();
}

 
 

MERGING OF ARRAYS 1

/* PROGRAM TO MERGE TWO ARRAYS ACCORDING TO A GIVEN ORDER

ARRAY 1:-     3 4 5 6
ARRAY 2:-     8 7 4 9
RESULTANT:-    3 4 5 6 9 4 7 8
*/

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int arr1[100],arr2[100],res[200];
int size1,size2;

cout<<“\nEnter the size of the first array\n”;
cin>>size1;
cout<<“\nEnter the size of the second array\n”;
cin>>size2;
cout<<“\nInput first array\n”;

for(int i = 0; i<size1; i++)
{
cin>>arr1[i];
}
cout<<“\nInput second array\n”;

for(i = 0; i<size2; i++)
{
cin>>arr2[i];
}

for(i = 0; i<size1; i++)
{
res[i] = arr1[i];
}
for(int j = 0; j<size2; j++)
{
res[i] = arr2[size2 – 1 – j];
i++;
}

cout<<“\nThe merged array is\n”;

for(i = 0; i< size1+size2; i++)
{
cout<<res[i]<<‘ ‘;
}

getch();
}