"How will you merge these two arrays? Write the program
Array: A 1 18 22 43
Array: B 3 4 6 20 34 46 55
Output Array: C 1 3 4 6 18 20 22 34 43 46 55"

Answers were Sorted based on User's Feedback



"How will you merge these two arrays? Write the program Array: A 1 18 22 43 Array: B 3 4 6 ..

Answer / arun

/*Program to merge two array & show them in shorted form */
#include<stdio.h>
#include<conio.h">

#define max 10

void main()
{
int a[max],b[max],c[2*max],i,j,k,n,m;
clrscr();
printf("size of array a:");
scanf("%d",&n);

printf("enter a elements: \n");
for(i=0;i<n;i++)

scanf("%d", &a[i]);


printf("size of array b:");
scanf("%d",&m);

printf("enter a elements: \n");
for(i=0;i<m;i++)

scanf("%d", &b[i]);




for(i=0,j=0,k=0; i<n && j<m; k++)

if(a[i]<b[j])

c[k] = a[i++];

else

c[k]=b[j++];

while(i<n)

c[k++] = a[i++];


while(j<m)

c[k++] = b[j++];


printf("\n array c:\n");
for(i=0;i<(m+n); i++)
printf("%d", c[i]);
getch();

}

Is This Answer Correct ?    19 Yes 7 No

"How will you merge these two arrays? Write the program Array: A 1 18 22 43 Array: B 3 4 6 ..

Answer / naman patidar

above problem can be solved using marge sort technique.
here i am giving a solution in java.

public class MargeSort {
public static void main(String[] args) {
int a[] = { 2, 5, 7, 9, 10, 15 };
int b[] = { 1, 3, 4, 5, 12, 14 };
int c[] = new int[a.length + b.length];
int aIndex = 0, bIndex = 0, cIndex = 0;

while (aIndex < a.length && bIndex < b.length) {
if (a[aIndex] < b[bIndex]) {
c[cIndex++] = a[aIndex++];
} else {
c[cIndex++] = b[bIndex++];
}
}
if (aIndex < a.length) {
while (aIndex < a.length) {
c[cIndex++] = a[aIndex++];
}
}
if (bIndex < b.length) {
while (bIndex < b.length) {
c[cIndex++] = b[bIndex++];
}
}
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}
}
}

Is This Answer Correct ?    12 Yes 7 No

"How will you merge these two arrays? Write the program Array: A 1 18 22 43 Array: B 3 4 6 ..

Answer / sandeep

#include<stdio.h>
#include<conio.h">

#define max 10

void main()
{
int a[max],b[max],c[2*max],i,j,k,n,m;
clrscr();
printf("size of array a:");
scanf("%d",&n);

printf("enter a elements: \n");
for(i=0;i<n;i++)

scanf("%d", &a[i]);


printf("size of array b:");
scanf("%d",&m);

printf("enter a elements: \n");
for(i=0;i<m;i++)

scanf("%d", &b[i]);




for(i=0,j=0,k=0; i<n && j<m; k++)

if(a[i]<b[j])

c[k] = a[i++];

else

c[k]=b[j++];

while(i<n)

c[k++] = a[i++];


while(j<m)

c[k++] = b[j++];
for(i=0;i<m+n-1;i++) //place each element in to correct position
{
for(j=i+1;j<m+n;j++)
{
if(c[i]>c[j]) //swapping
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}


printf("\n array c:\n");
for(i=0;i<(m+n); i++)
printf("%d", c[i]);
getch();

}

Is This Answer Correct ?    10 Yes 7 No

"How will you merge these two arrays? Write the program Array: A 1 18 22 43 Array: B 3 4 6 ..

Answer / manish podiyal

#include<iostream.h>
class array
{
int a[15],b[15],c[15];
public:
void setdata()
{
a[10]={1,18,22,43,NULL};
b[10]={3,4,6,20,34,46,55,NULL};
}
void output()
{
while(a[i]!=NULL)
{
for(i=0;i<15;i++)
{
if(a[i]< b[i])
{
c[i]=a[i];
i++;
}
else if(a[i] > b[i])
{
c[i]=b[i];
i++;
}
}
}
if(a[i]==NULL)
{
c[i]=b[i];
i++;
}
}
};
void main()
{
array o;
o.setdata();
o.output();
}

Is This Answer Correct ?    28 Yes 26 No

"How will you merge these two arrays? Write the program Array: A 1 18 22 43 Array: B 3 4 6 ..

Answer / mms zubeir

Though it seems bigger, I feel it's clean. See below:

void sortArray(int*array, int length)
{
if(length > 0)
for(int index = 0; index < length; ++index)
for(int cmpIndex = index + 1;
cmpIndex < length; ++cmpIndex)
{
int temp = 0;
if(array[index] > array
[cmpIndex])
{
temp = array[index];
array[index] = array
[cmpIndex];
array[cmpIndex] =
temp;
}
}
}

void addArray(int arrFirst[], int firstLen, int arrSecond
[], int secondLen, int arrResult[], int resultLen)
{
int resultIndex = 0;

for(int firstIndex = 0; firstIndex< firstLen;
++firstIndex)
arrResult[resultIndex++] = arrFirst
[firstIndex];

for(int secondIndex = 0; secondIndex < secondLen;
++secondIndex)
arrResult[resultIndex++] = arrSecond
[secondIndex];
}

void mergeArray(int arrFirst[], int firstLen, int arrSecond
[], int secondLen, int arrResult[], int resultLen)
{
addArray(arrFirst, firstLen, arrSecond, secondLen,
arrResult, resultLen);
sortArray(arrResult, resultLen);
}

Is This Answer Correct ?    11 Yes 9 No

"How will you merge these two arrays? Write the program Array: A 1 18 22 43 Array: B 3 4 6 ..

Answer / sampath

hi friends !
i use java code it simple idea

class Sort
{
public static void main()
{
int A[]={1,18,22,43};
int B[]={3,4,6,20,34,46,55}

int lenA=A.length;
int lenB=B.length;

int n=lenA+lenB; //find the array size


int C[]=new int[n];
int i,j,temp;

for(i=0;i<n;i++) //copy the two array into C array
{
if(i<lenA)
C[i]=A[i];
else
C[i]=B[i-lenA];
}


for(i=0;i<n;i++) //place each element in to correct position
{
for(j=0;j<n;j++)
{
if(c[i]>c[j]) //swapping
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
for(i=0;i<n;i++)// display output
{
System.Out.Print(c[i]+",");
}
}

Is This Answer Correct ?    5 Yes 5 No

"How will you merge these two arrays? Write the program Array: A 1 18 22 43 Array: B 3 4 6 ..

Answer / a

a

Is This Answer Correct ?    4 Yes 6 No

"How will you merge these two arrays? Write the program Array: A 1 18 22 43 Array: B 3 4 6 ..

Answer / naman patidar

public class MargeSort {
public static void main(String[] args) {
int a[] = { 2, 5, 7, 9, 10, 15 };
int b[] = { 1, 3, 4, 5, 12, 14 };
int c[] = new int[a.length + b.length];
int aIndex = 0, bIndex = 0, cIndex = 0;

while (aIndex < a.length && bIndex < b.length) {
if (a[aIndex] < b[bIndex]) {
c[cIndex++] = a[aIndex++];
} else {
c[cIndex++] = b[bIndex++];
}
}

while (aIndex < a.length) {
c[cIndex++] = a[aIndex++];
}
while (bIndex < b.length) {
c[cIndex++] = b[bIndex++];
}
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}
}
}

Is This Answer Correct ?    7 Yes 14 No

"How will you merge these two arrays? Write the program Array: A 1 18 22 43 Array: B 3 4 6 ..

Answer / foreverkushal

void MergeArray(int *A1, int A1Count, int *A2, int A2Count,
int *A3, int A3Count)
{
int i = 0, j = 0, k = 0;
while(i != A1Count && j != A2Count)
{
if (A1[i] < A2[j]) A3[k++] = A1[i++];
else A3[k++] = A2[j++];
}
if (i != A1Count)
{
while (i < A1Count) A3[k++] = A1[i++];
}
if (j != A2Count)
{
while (i < A2Count) A3[k++] = A2[j++];
}
}

Is This Answer Correct ?    23 Yes 43 No

Post New Answer

More C++ General Interview Questions

Mention the ways in which parameterized can be invoked. Give an example of each.

0 Answers  


When is dynamic checking necessary?

0 Answers  


What is a dynamic binding in c++?

0 Answers  


How do I write a c++ program?

0 Answers  


Do class declarations end with a semicolon?

0 Answers  






What are the conditions that have to be met for a condition to be an invariant of the class?

1 Answers  


what is an array

17 Answers  


Can malloc be used in c++?

0 Answers  


Does c++ cost money?

0 Answers  


What are the benefits of oop in c++?

0 Answers  


Hi i need to Acess a variable "int Intval" in the below mentioned code .How to Access it guys i am waiting for your reply

3 Answers  


What happens if an exception is throws from an object's constructor and from object's destructor?

3 Answers   TCS,


Categories