"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
Explain the difference between overloading and overriding?
Differences between private, protected and public and give examples.
Which compiler does turbo c++ use?
Can we declare a base-class destructor as virtual?
Explain what happens when a pointer is deleted twice?
Explain the properties and principles of oop.
What is stoi in c++?
What is runtime polymorphism in c++?
What are the five basic elements of a c++ program?
write a program that withdrawals,deposits,balance check,shows mini statement. (using functions,pointers and arrays)
When is dynamic checking necessary?
What doescout<<(0==0) print out a) 0 b) 1 c) Compiler error: Lvalue required
What are the benefits of oop in c++?
What is :: operator in c++?
How much do coding jobs pay?