Hi, I have been struggling to conceptually understand this problem with functions, arrays, and returning a pointer to the dynamically allocated array that contains the merged array. I would appreciate it if you simplify the core concept in detail. Thank you. Problem: Develop a function that receives two arrays using pass by pointer and their sizes and merges them into a third array. Return the merged array.
#Merging Two Arrays into a Third Using Pointer in C++
1 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
hello?
please @ me once you have replied
No one is going to help you unless you show existing code

is it a requirement? i didnt try coding it yet because the logic is confusing
Yep
okay
i wasnt sure if i should post a picture instead so i copied pasted it from VS
mhmhm since youre talking about merging the arrays I assume you want the output array to also be sorted?
If you just want to combine the arrays you just missed that the secondArray index should also start at 0
i dont think the original problem statement provided did not specify that the merged array needs to be sorted. j =0 ? ok i will fix that
i wasnt sure if i understood the concept tho
!f
#include<iostream>
using namespace std;
int* CombineArrays(int* firstArray, int len1, int* secondArray, int len2) {
int* CombinedArray = new int[len1 + len2];
int mergeIndex = 0;
for (int i = 0; i < len1; i++) {
CombinedArray[mergeIndex++] = firstArray[i];
}
for (int j = 1; j < len2; j++) {
CombinedArray[mergeIndex++] = secondArray[j];
}
return CombinedArray; // Return combined array
}
int main() {
int firstArray[] = {1, 3, 5}; // First array
int secondArray[] = {2, 4, 6, 8, 10}; // Second array
int* arrayAfterMerge = CombineArrays(firstArray, 3, secondArray, 5);
for (int i = 0; i < 8; i++) {
cout << arrayAfterMerge[i] << " ";
}
cout << endl;
delete[] arrayAfterMerge;
return 0;
}
thanks
how do you want to implement the merge
if we are just talking about combining the arrays
just from looking at the code it looks like you got a pretty good idea of the algorithm
the first element of secondArray doesn't merge right?
combining the elements of the first array with the second array into a new third array. i think
;compile c++ -includeiostream
Program Output
1 3 5 4 6 8 10 0
gutoguto08 | 33ms | c++ | x86-64 gcc 13.2 | godbolt.org
i hope so
yeah, 2 isn't in here
Usually the term merge is used when talking about combining the arrays while preserving the order
oh okay i re-write some parts of the code
your second for loop statement is initialized to 1
can you also put that change in the code snipped you sent? #1220122451409895565 message
do u want me to send the code again fixed
edit the previous message
you can also do that
#include<iostream>
using namespace std;
int* CombineArrays(int* firstArray, int len1, int* secondArray, int len2)
{
int* CombinedArray= new int[len1 + len2];
int mergeIndex = 0;
for (int i = 0; i < len1; i++) {
CombinedArray[mergeIndex++] = firstArray[i];
}
for (int j = 0; j < len2; j++) {
CombinedArray[mergeIndex++] = secondArray[j];
}
return CombinedArray;
}
int main() {
int firstArray[] = { 1, 3, 5 };
int secondArray[] = { 2, 4, 6, 8, 10 };
int* arrayAfterMerge = CombineArrays(firstArray, 3, secondArray, 5);
for (int i = 0; i < 8; i++) {
cout << arrayAfterMerge[i] << " ";
}
cout << endl;
delete[] arrayAfterMerge;
return 0;
}