"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

what is importance of data sturture in a programming language?

22 Answers   L&T, TCS, Wipro,


Which one of the following describes characteristics of "protected" inheritance? a) The base class has access only to the public or protected members of the derived class. b) The derived class has non-public, inheritable, access to all but the private members of the base class. c) The derived class has access to all members of the base class. d) The private members of the base class are visible within the derived class. e) Public members of the derived class are privately accessible from the base class.

4 Answers   Quark,


Write any small program that will compile in "C" but not in "C++"

10 Answers   Honeywell,


Assume an array of structure is in order by studentID field of the record, where student IDs go from 101 to 500. Write the most efficient pseudocode algorithm you can to find the record with a specific studentID if every single student ID from 101 to 500 is used and the array has 400 elements. Write the most efficient pseudocode algorithm you can to find a record with a studentID near the end of the IDs, say in the range from 450 to 500, if not every single student ID in the range of 101 to 500 is used and the array size is only 300

0 Answers  


Is c++ free?

0 Answers  






What is a mutex and a critical section.Whats difference between them?How do each of them work?

4 Answers   CTS,


What is prototype for that c string function?

0 Answers  


What will strcmp("Astring", "Astring"); return a) A positive value b) A negative value c) Zero

0 Answers  


What is the difference between interpreters and compilers?

0 Answers  


Why preincrement operator is faster than postincrement?

5 Answers  


founder of c++

7 Answers   Microtek, TCS, TeleCommand,


Define private, protected and public access control.

0 Answers  


Categories