#Merging Two Arrays into a Third Using Pointer in C++

1 messages · Page 1 of 1 (latest)

iron galleon
#

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.

uneven tulipBOT
#

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.

iron galleon
#

hello?

iron galleon
#

please @ me once you have replied

bold shore
#

No one is going to help you unless you show existing code

bold shore
iron galleon
bold shore
#

Yep

iron galleon
#

okay

iron galleon
#

i wasnt sure if i should post a picture instead so i copied pasted it from VS

copper jungle
#

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

iron galleon
#

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

bold shore
#

!f

uneven tulipBOT
#

#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;
}
tun tun
iron galleon
#

thanks

bold shore
#

how do you want to implement the merge

copper jungle
#

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

bold shore
#

the first element of secondArray doesn't merge right?

iron galleon
light heathBOT
#
Program Output
1 3 5 4 6 8 10 0
bold shore
copper jungle
#

Usually the term merge is used when talking about combining the arrays while preserving the order

iron galleon
#

oh okay i re-write some parts of the code

bold shore
#

your second for loop statement is initialized to 1

iron galleon
#

yea chris told me to change it

#

i will initialize it to 0

bold shore
iron galleon
#

do u want me to send the code again fixed

bold shore
#

edit the previous message

bold shore
iron galleon
#

#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;

}

uneven tulipBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
#

@iron galleon Has your question been resolved? If so, type !solved :)

iron galleon
#

thanks guys

#

!solved