"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
What is object in c++ example?
What are c++ files?
What is exception handling? Does c++ support exception handling?
What is extern c++?
What are the basics of local (auto) objects?
Can constructor be static in c++?
What is namespace & why it is used in c++?
Why do we need function?
Can notepad ++ run c++?
can any one help to find a specific string between html tags
which is changed to a sting..
weather.html looks (for location) is
What is a type library? What is searching? What does obj stand for? How are Structure passing and returning implemented by the compiler? What is a constructor in c++ with example?