"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"

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Give 10 points of differences between C & C++.

623


What are the two types of polymorphism?

591


Does improper inheritance have a potential to wreck a project?

630


Write a program using GUI concept for the scheduling algorithms in Operating system like SJF,FCFS etc..

3358


Why c++ is created?

576






Is string an object in c++?

657


Which programming language's unsatisfactory performance led to the discovery of c++?

804


How can virtual functions in c++ be implemented?

610


What are pointer-to-members in C++? Give their syntax.

617


What is a constructor in c++ with example?

578


If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which?

1005


Is java made in c++?

583


What do you mean by inheritance in c++?

606


Can comments be longer than one line?

623


What is binary object model?

600