C++ Merge Arrays!

NomisST

Member
May 4, 2002
191
0
0
Hello everyone, this is my third post for help for my Programming Meth Class. My class has gotten a problem in Lab where only 3/the entire class figured out. It's due Sunday and I just don't know what to do. Well I should say I don't have time to do it because I have an exam on Sunday plus this assignment being due right after the exam, but can anyone give me help or teach me how to merge these arrays. My program looks like this. I don't want anyone to fiddle around with the main function. I just don't know what to do in the void merge function. Also we are supposed to use strcpy and strcmp functions. I looked on google on how to merge arrays but they do it without the functions. Can anyone help??? Much appreciated and would help this future engineer out a lot.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cstring>
using std::strcpy;
using std::strcmp;


//write the prototype of the merge fucntion here
void merge ( char * [ ], int, char * [ ], int, char * [ ] )

//main function for testing the merge routine
int main()
{
char *arr1[20];
char *arr2[20];
char *arr3[40];

//write code to allocate memory for all elements in arr1 and arr2
for ( i = 0; i < 20; i++ )
{
char arr1[ i ] = new char[ ];
char arr2[ i ] = new char[ ];
}
for ( l = 0; l < 40; l++ )
{
char arr3[ j ] = new char[ ];
}

//initialize arr1 and arr2
arr1[0]="ant";
arr1[1]="bird";
arr1[2]="dog";
arr1[3]="zebra";

arr2[0]="cat";
arr2[1]="monkey";
arr2[2]="octopus";
arr2[3]="parrot";


//write code to call merge rountie to merge arr1 and arr2 to arr3
merge( arr1, 4, arr2, 4, arr3 )


//write code to display all elements in arr3 one by one
for ( int j = 0; j < 8; j++ )
cout << arr3[ j ];


//write code to release the mememory of arr1, arr2, and arr3
for ( j = 4; j < 20; j++ )
{
delete arr1[ j ];
delete arr2[ j ];
}
for ( k = 8; k < 40; k++ )
{
delete arr3[ k ];
}

return 0;
}


/*This function merges arr1 and arr2 into a final array arr3,
which should be ascending order too.

Assumptions:
1) arr1 and arr2 are two characger pointer arrays, which
are both in ascending order

2) arr1 and arr2 have been initialized and contain size1
and size2 number of elements respecitively

3) arr3 has not been initialized
*/
void merge( char *arr1[], int size1, char *arr2[], int size2, char *arr3[] )
{
//write code to implement the merge routine
}
 

Spydermag68

Platinum Member
Apr 5, 2002
2,616
99
91
k=0
while (k < (i+j))
{
If arr1 > arr2[j]
{
arr3[k++] = arr2[j++]
}
else
{
arr3[k++] = arr1[i++]
}
}

 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
I assume the result of the merge would be that arr3[0] thru arr3[19] = arr1[0] thru arr1[19], and arr3[20] thru arr3[39] = arr2[0] thru arr2[19]?

Simply loop through each array (arr1 and arr2), and create a new element in arr3 per each one in each array. The contents of arr1(i) are being copied to arr3(j) as you can see in the code.

Function call: merge(arr1,20,arr2,20,arr3);
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
I didn't think you had to loop through the arrays to initialize them. Code:

//write code to allocate memory for all elements in arr1 and arr2
for ( i = 0; i < 20; i++ )
{
char arr1[ i ] = new char[ ];
char arr2[ i ] = new char[ ];
}
for ( l = 0; l < 40; l++ )
{
char arr3[ j ] = new char[ ];
}

I thought you just needed to do this:
char * arr1;
arr1 = new char[20];
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: clamum
I thought you just needed to do this:
char * arr1;
arr1 = new char[20];

That'd just allocate 20 characters in one string (arr1).
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Oooh, OK, I didn't realize at first when I saw the code that he was storing strings in the array. :eek:
 

NomisST

Member
May 4, 2002
191
0
0
OMG, I'm an idiot. Not just merge them, but merge while they sort...gah I was wondering why no one had any strcmp or strcpy functions. The merge function has to merge the arrays and sort them while merging...this is why no one could do it. It's easy to merge the arrays, but how do you sort them while it merges in the same function??!?!?! Thanks for everyone that replied!