Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: C++ Help !!

1. C++ Help !!

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

2. 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.

3. 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 ?

4. 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.

5. 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

6. 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
Code:
```Compare(void *i, void *j)
{
return ((int) *i > (int) *j);
}```
Then you should call MinMax() as follows:
Code:
`MinMax(pointerToArray, low, high, Compare, min, max)`
You may not have converted void * to the appropriate type, hence the errors.

7. 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

8. 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.

9. 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

10. 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:
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)
{
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);
}
}```

11. 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

12. Do you mean pointerToArray returns the size of array - 1?

13. I mean the returned values of 'min' and 'max' from the function MinMax are the size of the array-1

14. 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]

15. '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 ?

Page 1 of 2 12 Last

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•