"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

How is objective c different from c++?

987


What is pointer to array in c++?

797


What are the characteristics of friend functions?

753


What is the difference between the indirection operator and the address of oper-ator?

813


Explain linked list using c++ with an example?

792


What are compilers in c++?

820


Snake Game: This is normal snake game which you can find in most of the mobiles. You can develop it in Java, C/C++, C# or what ever language you know.

2752


Should the member functions which are made public in the base class be hidden?

757


What are the types of pointer?

770


What is the difference between strcpy() and strncpy()?

834


What is constructor c++?

852


What is an arraylist c++?

889


Which of the following is not a valid declaration for main() a) int main() b) int main(int argc, char *argv[]) c) They both work

823


Who was the creator of c++?

756


Write syntax to define friend functions in C++.

802