"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
Why should you learn c++?
To what does “event-driven” refer?
What is singleton pattern in c++?
What is the use of structure in c++?
Which format specifier is used for printing a pointer value?
What do you mean by vtable and vptr in c++?
What are shallow and deep copies?
What does iomanip mean in c++?
What is the purpose of the "delete" operator?
Can a new be used in place of old mallocq? If yes, why?
What is difference between malloc()/free() and new/delete?
What is a loop? What are different types of loops in c++?
Is map ordered c++?
How do we balance an AVL Tree in C++?
How the delete operator differs from the delete[]operator?