"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
Give 10 points of differences between C & C++.
What are the two types of polymorphism?
Does improper inheritance have a potential to wreck a project?
Write a program using GUI concept for the scheduling algorithms in Operating system like SJF,FCFS etc..
Why c++ is created?
Is string an object in c++?
Which programming language's unsatisfactory performance led to the discovery of c++?
How can virtual functions in c++ be implemented?
What are pointer-to-members in C++? Give their syntax.
What is a constructor in c++ with example?
If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which?
Is java made in c++?
What do you mean by inheritance in c++?
Can comments be longer than one line?
What is binary object model?