...

# C++ Help !!

dave9999
01-04-2003, 06:45 PM
Hello, I need to build a recursive function that searches for minimum and maximum numbers in an array that contains data from the same type.

The decleration to the function should be:

void MinMax(void *pointerToArray,unsigned low,unsigned high,
int(*PointerToCompareFunction)(void*,void*),
int &min,int &max)

while low and high are the bounderies of the search (indexes)

How do I do that and how do I write the PointerToCompareFunction ?

Thankyou very very much

Dave

Spookster
01-04-2003, 10:52 PM
This sounds just like a school assignment. We will not do your homework for you.

I recall doing these same assignments when I was in college. The point to these recursive problems the teachers give is to understand how recursion works. Once you know that you can write it rather easily.

Read up on recursion and then if you have a specific question how recursion works or have a specific question on C++ syntax then ask away but as I said before we will not do it for you.

dave9999
01-05-2003, 01:06 AM
My problem is not about the recursion part. Recursion is rather easy. The problem is in pointers to function. I am having trouble
to understand how to write a function which recieves void arguments and how to treat it. The point in the question is to create a 'global' function for every array available.

Can someone guide me or tell me how to buid such a function ?

codefox
01-05-2003, 06:18 PM
I understand that int(*PointerToCompareFunction)(void*,void*) is a pointer to a function accepting two void * parameters and returning an int but what are the two void * parameters?

BTW, you could always type-cast a void * to and int * or any other pointer variable as long as you know that you are typecasting it to the right type.

dave9999
01-05-2003, 10:10 PM
The function is supposed to compare between two elements and return if the first one is bigger than the second.

In my function, when I write:

if(pointerToCompareFunction(&pointerToArray[low],&max)==TRUE)

It gives two errors:

1) size of 'void' is unknown or zero
2) Not an allowed type

What is the problem ?

Thankyou

codefox
01-06-2003, 10:28 AM
Have you written the actual comparison function? If you'd written a function that returns if the first parameter is greater than the second one and want to use it as your comparison function in the MinMax function then you should pass its address to the MinMax function. For example, if you have the comparison func as

Compare(void *i, void *j)
{
return ((int) *i > (int) *j);
}

Then you should call MinMax() as follows:

MinMax(pointerToArray, low, high, Compare, min, max)

You may not have converted void * to the appropriate type, hence the errors.

dave9999
01-06-2003, 01:51 PM
I have written the Compare function and there is no error in it.
The following two lines cause errors:

if(pointerToCompareFunction(&min,&pointerToArray[low])==TRUE)
if(pointerToCompareFunction(&pointerToArray[low],&max)==TRUE)

It gives the errors:

- Size of type 'void' is unknown or zero
- Not an allowed type

Each error repeats twice..

What seems to be the problem ?

Thankyou

codefox
01-06-2003, 05:12 PM
Try
if(pointerToCompareFunction((void *)&min,(void *)&pointerToArray[low])==TRUE) since your function prototype says the arguments are of type void *. If you still get errors, please post your code.

dave9999
01-06-2003, 08:40 PM
I did that and got the same errors.
Here is my code:

int pointerToCompareFunction(void *first,void *second)
{
long double Element1,Element2;
Element1=*(long double*)first;
Element2=*(long double*)second;
return(Element1>Element2?1:0);
}

void MinMax(void *pointerToArray,unsigned low,unsigned high,
int(*pointerToCompareFunction)(void*,void*),int &min, int &max)
{
if(low!=high)
{
if(pointerToCompareFunction((void *)&pointerToArray[low],(void *)&max)==TRUE)
max=low;
if(pointerToCompareFunction((void *)&min,(void *) &pointerToArray[low])==TRUE)
min=low;
low++;
MinMax(pointerToArray,low,high,pointerToCompareFunction,min,max);
}
}

Thankyou
Dave

codefox
01-07-2003, 01:56 PM
You cannot pass &pointerToArray[low] since pointerToArray is a pointer to void. You have to first convert it to a meaningful datatype before passing it to the compare function. Try this to compile without errors:
int pointerToCompareFunction(void *first, void *second)
{
long double Element1, Element2;
Element1 = *((long double *) first);
Element2 = *((long double *) second);
return (Element1 > Element2 ? 1 : 0);
}

void MinMax(void *pointerToArray,
unsigned low,
unsigned high,
int (*pointerToCompareFunction)(void *, void *),
int min,
int max)
{
long double *dArray = (long double *) pointerToArray;
if (low != high) {
if (pointerToCompareFunction(&dArray[low], (void *) &max))
max = low;
if (pointerToCompareFunction((void *) &min, &dArray[low]))
min = low;
low++;
MinMax(pointerToArray, low, high, pointerToCompareFunction, min, max);
}
}

dave9999
01-07-2003, 02:56 PM
The code give no errors but it always return the size of the array minus one. Another problem I found is that 'min' and 'max' are indexes and thus cannot be compared with values in the array.
I re-wrote the code, but it still returns the size of the array minus one:

void MinMax(void *pointerToArray,unsigned low,unsigned high,
int(*pointerToCompareFunction)(void*,void*),int &min,int &max)
{
long double *TheArray =(long double *)pointerToArray;
if(low!=high)
{
if(pointerToCompareFunction(&TheArray[low],&TheArray[max]))
max=low;
if (pointerToCompareFunction((&TheArray[min]),&TheArray[low]))
min=low;
low++;
MinMax(pointerToArray,low,high,pointerToCompareFunction,min,max);
}
}

What is the problem ?

Thankyou
Dave

codefox
01-08-2003, 06:28 AM
Do you mean pointerToArray returns the size of array - 1?

dave9999
01-08-2003, 01:24 PM
I mean the returned values of 'min' and 'max' from the function MinMax are the size of the array-1

codefox
01-08-2003, 02:38 PM
if(pointerToCompareFunction(&TheArray[low],&TheArray[max]))
max=low;
if (pointerToCompareFunction((&TheArray[min]),&TheArray[low]))
min=low;

In the above lines are you sure it is max = low and min = low? Or is it max = TheArray[low] and min = TheArray[low]

dave9999
01-08-2003, 06:29 PM
'min' and 'max' stores indexes, not values.
Logically, the ode is correct I think.
Why does it return the index of the last elemnt-1 ?

codefox
01-09-2003, 07:57 AM
Have no idea :(, maybe if you could post the whole program, I'll try executing and see.

dave9999
01-09-2003, 12:23 PM
Here is my program. The function MinMax is supposed to save the minimum and the maximum in the array to 'min' and 'max'
The problem is that it always returns the 'size of array-1'
Please try to find the problem, I need to hand it in for today !

#include <iostream.h>
#define TRUE 1

int pointerToCompareFunction(void *first,void *second)
{
long double Element1,Element2;
Element1=*(long double*)first;
Element2=*(long double*)second;
return(Element1>Element2?1:0);
}

void MinMax(void *pointerToArray,unsigned low,unsigned high,
int(*pointerToCompareFunction)(void*,void*),int &min,int &max)
{
long double *TheArray =(long double *)pointerToArray;
if(low!=high)
{
if(pointerToCompareFunction(&TheArray[low],&TheArray[max]))
max=low;
if (pointerToCompareFunction((&TheArray[min]),&TheArray[low]))
min=low;
low++;
MinMax(pointerToArray,low,high,pointerToCompareFunction,min,max);
}
}

void main()
{
int array[]={999,7,89,134,355,6,98,100};
int min=0,max=0;
MinMax(array,0,7,pointerToCompareFunction,min,max);
cout<<"Min: "<<min<<" Max: "<<max;
cin.get();
}

Thankyou very much
Dave

codefox
01-09-2003, 03:46 PM
The array in main() is of type int whereas in the functions you use long double. I don't know why you use it that way, but it works fine when I have all of them as int or double. Here's the code I used. I compiled in turbo c++ ver.3. since i had problems with using long double, I used double throughout.
#include <iostream.h>
#define TRUE 1

int pointerToCompareFunction(void *first, void *second)
{
double Element1, Element2;
Element1 = *(double *) first;
Element2 = *(double *) second;
return (Element1 > Element2 ? 1 : 0);
}

void MinMax(void *pointerToArray,unsigned low, unsigned high, int (*pointerToCompareFunction)(void*,void*), int &min, int &max)
{
double *TheArray =(double *) pointerToArray;

if (low!=high) {
if (pointerToCompareFunction(&TheArray[low], &TheArray[max]))
max=low;
if (pointerToCompareFunction(&TheArray[min], &TheArray[low]))
min=low;
low++;
MinMax(pointerToArray, low, high, pointerToCompareFunction, min, max);
}
}

void main()
{
double array[] = {999, 7, 89, 134, 355, 6, 98, 100};
int min = 0, max = 0;

MinMax(array, 0, 7, pointerToCompareFunction, min, max);

cout << "Min: " << array[min] << " Max: " << array[max];
cin.get();
}

Declare array[], TheArray, Element1, and Element2 of the same datatype.

dave9999
01-09-2003, 03:57 PM
Thankyou very much
It works :)

codefox
01-09-2003, 04:02 PM
At last, phew...:)