Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com C++ Help !!

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-04-2003, 06:45 PM PM User | #1 dave9999 New Coder   Join Date: Jan 2003 Posts: 10 Thanks: 0 Thanked 0 Times in 0 Posts 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
 01-04-2003, 10:52 PM PM User | #2 Spookster Supreme Overlord     Join Date: May 2002 Location: Marion, IA USA Posts: 6,264 Thanks: 4 Thanked 82 Times in 81 Posts 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. __________________ Spookster CodingForums Supreme Overlord All Hail Spookster
 01-05-2003, 01:06 AM PM User | #3 dave9999 New Coder   Join Date: Jan 2003 Posts: 10 Thanks: 0 Thanked 0 Times in 0 Posts 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 ?
 01-05-2003, 06:18 PM PM User | #4 codefox Regular Coder   Join Date: Nov 2002 Location: India Posts: 159 Thanks: 0 Thanked 0 Times in 0 Posts 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.
 01-05-2003, 10:10 PM PM User | #5 dave9999 New Coder   Join Date: Jan 2003 Posts: 10 Thanks: 0 Thanked 0 Times in 0 Posts 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
 01-06-2003, 10:28 AM PM User | #6 codefox Regular Coder   Join Date: Nov 2002 Location: India Posts: 159 Thanks: 0 Thanked 0 Times in 0 Posts 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.
 01-06-2003, 01:51 PM PM User | #7 dave9999 New Coder   Join Date: Jan 2003 Posts: 10 Thanks: 0 Thanked 0 Times in 0 Posts 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
 01-06-2003, 05:12 PM PM User | #8 codefox Regular Coder   Join Date: Nov 2002 Location: India Posts: 159 Thanks: 0 Thanked 0 Times in 0 Posts 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.
 01-06-2003, 08:40 PM PM User | #9 dave9999 New Coder   Join Date: Jan 2003 Posts: 10 Thanks: 0 Thanked 0 Times in 0 Posts 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
 01-07-2003, 01:56 PM PM User | #10 codefox Regular Coder   Join Date: Nov 2002 Location: India Posts: 159 Thanks: 0 Thanked 0 Times in 0 Posts 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); } }```
 01-07-2003, 02:56 PM PM User | #11 dave9999 New Coder   Join Date: Jan 2003 Posts: 10 Thanks: 0 Thanked 0 Times in 0 Posts 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
 01-08-2003, 06:28 AM PM User | #12 codefox Regular Coder   Join Date: Nov 2002 Location: India Posts: 159 Thanks: 0 Thanked 0 Times in 0 Posts Do you mean pointerToArray returns the size of array - 1?
 01-08-2003, 01:24 PM PM User | #13 dave9999 New Coder   Join Date: Jan 2003 Posts: 10 Thanks: 0 Thanked 0 Times in 0 Posts I mean the returned values of 'min' and 'max' from the function MinMax are the size of the array-1
01-08-2003, 02:38 PM   PM User | #14
codefox
Regular Coder

Join Date: Nov 2002
Location: India
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
 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]

 01-08-2003, 06:29 PM PM User | #15 dave9999 New Coder   Join Date: Jan 2003 Posts: 10 Thanks: 0 Thanked 0 Times in 0 Posts '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 ?

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 05:41 AM.