1.write a program to merge the arrays
2.write efficient code for extracting unique elements from a
sorted list of array?

Answer Posted / nishant chauhan

//#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;

int merge(int A[],int B[],int C[],int m,int n)
{
int i=0, j=0, k=0,len=0;
while (i < m && j < n)
{
if (A[i] <B[j])
{
C[k] = A[i];
i++;
len++;
}
else if(A[i] >B[j])
{
C[k] = B[j];
j++;
len++;
}
else
{
C[k]=A[i];
i++;j++;
len++;
}
k++;
}

if (i < m)
{
for (int p = i; p < m; p++)
{
C[k] = A[p];
k++;len++;
}

}
else
{
for (int p = j; p < n; p++)
{
C[k] = B[p];
k++; len++;
}
}
return len;
}

main ()
{
int a[20],b[20],c[30],m,n;
cout<<"enter the length of first array: ";
cin>>m;
cout<<"enter the array: ";
for(int i=0;i<m;i++)
cin>>a[i];
cout<<"enter the length of second array: ";
cin>>n;
cout<<"enter the array: ";
for(int j=0;j<n;j++)
cin>>b[j];
int length=merge(a,b,c,m,n);
cout<<"resulting merging array is: ";
for(int k=0;k<length;k++)
cout<<c[k]<<" ";
getch();
}

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why should I use standard library functions instead of writing my own?

800


What does calloc stand for?

741


Why double pointer is used in c?

658


Why clrscr is used in c?

677


What are the different properties of variable number of arguments?

766






why to assign a pointer to null sometimes??how can a pointer we declare get assigned with a garbage value by default???

1609


What is the use of header files?

696


What is the best style for code layout in c?

710


What is the use of c language in real life?

636


Explain which function in c can be used to append a string to another string?

702


Which header file is used for clrscr?

674


The performance of an operation in several steps with each step using the output of the preceding step a) recursion b) search c) call by value d) call by reference

767


Input is "rama loves rajesh and rajesh Loves rama also and rajesh wear gloves and bloves" To print output is count the numbers of times repeted the word love without case sensitive.

1698


How does sizeof know array size?

734


What is calloc malloc realloc in c?

688