Question #53767

Consider A, B, C as three arrays of size m,n and m + n respectively. Array A is stored in ascending order whereas array B is stored in descending order. Write a C++ program to produce a third array C, containing all the data of arrays A and B and arranged in descending order. Display the data of array C only.

Expert's answer

Solve

#include <iostream>

using namespace std;

int main()

{

int n=4;

int m=5;

int a[]={1,3,5,7};

int b[]={8,6,4,2,0};

int *c;

c=new int[n+m];

int i_a=n-1, i_b=0;

int i=0;

while((i_a>=0)&&(i_b<m))

{

if(a[i_a]<b[i_b])

{

c[i]=b[i_b];

i++;

i_b++;

}

else

{

c[i]=a[i_a];

i++;

i_a--;

}

}

while(i_a>=0)

{

c[i]=a[i_a];

i++;

i_a--;

}

while(i_b<m)

{

c[i]=b[i_b];

i++;

i_b++;

}

for(i=0;i<n+m;i++)

cout<<c[i]<<" ";

return 0;

}

#include <iostream>

using namespace std;

int main()

{

int n=4;

int m=5;

int a[]={1,3,5,7};

int b[]={8,6,4,2,0};

int *c;

c=new int[n+m];

int i_a=n-1, i_b=0;

int i=0;

while((i_a>=0)&&(i_b<m))

{

if(a[i_a]<b[i_b])

{

c[i]=b[i_b];

i++;

i_b++;

}

else

{

c[i]=a[i_a];

i++;

i_a--;

}

}

while(i_a>=0)

{

c[i]=a[i_a];

i++;

i_a--;

}

while(i_b<m)

{

c[i]=b[i_b];

i++;

i_b++;

}

for(i=0;i<n+m;i++)

cout<<c[i]<<" ";

return 0;

}

## Comments

Assignment Expert10.01.18, 15:49Yes

Rohit10.01.18, 09:04Is this complete answer??

## Leave a comment