"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
How is objective c different from c++?
What is pointer to array in c++?
What are the characteristics of friend functions?
What is the difference between the indirection operator and the address of oper-ator?
Explain linked list using c++ with an example?
What are compilers in c++?
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.
Should the member functions which are made public in the base class be hidden?
What are the types of pointer?
What is the difference between strcpy() and strncpy()?
What is constructor c++?
What is an arraylist c++?
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
Who was the creator of c++?
Write syntax to define friend functions in C++.