Ok so i have the following functions that does a recursive merge sort. I need to pass my arrays in by reference since I have many different arrays containing different things. Im comparing lists[index1].get_type() vs lists[index2].get_type() yet when i run this i am getting the same event with the same members. So i know i need to pass by reference using the * but apparently im missing it some wheres so i need some quick help. anyone?

void merge_sort(UTAevent to_sort[], int first, int last){
	if ( first < last ){
		merge_sort(to_sort, first, (first+last)/2);

		merge_sort(to_sort, (first+last)/2 + 1, last );

		merge( to_sort, first, ((first+last)/2), ((first+last)/2 + 1), last );
	}
}
void merge( UTAevent lists[], int first1, int last1, int first2, int last2){
	UTAevent temp[EVENTMAX];
	int index, index1, index2;
	int num;

	index = 0;
	index1 = first1;
	index2 = first2;
	num = last1 - first1 + last2 - first2 + 2;

	while ((index1 <= last1) && (index2 <= last2 )){
		if ( (lists[index1].get_type()) < (lists[index2].get_type()))
			temp[index++] = lists[index1++];
		else
			temp[index++] = lists[index2++];
	}
	if ( index1> last1)
		move( lists, index2, last2, temp, index);
	else
		move(lists, index1, last1, temp, index);

	move( temp, 0, num-1, lists, first1);
}
void move(UTAevent list1[], int first1, int last1, UTAevent list2[], int first2){
	while (first1 <= last1)
		list2[first2++] = list1[first1++];
}

If you want to leverage polymorphism you should be using an array of pointers to the base class. The array itself doesn't need to be passed by reference because it's already passed as a pointer to the first element (simulated pass-by-reference).

Edited 6 Years Ago by Narue: n/a

example?

Really?

UTAevent *to_sort[]

The array you're passing won't be the correct type anymore, so we're talking about completely changing any code that presently calls the sort by adding an extra level of indirection. I can't give you an example without doing it for you, and I can't do it for you (this is assuming that I would) without seeing all of your code.

Give it a try. If you can't do it without 100% hand holding, your program is probably too complex for your level.

Edited 6 Years Ago by Narue: n/a

This article has been dead for over six months. Start a new discussion instead.