Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Generic algorithms

Name: Anonymous 2008-11-28 11:31

Implement a single function that takes two arguments and returns the bigger of the two. Assume you don't know the type of the  arguments and you don't know if the types can be compared. Assume that if a type can be compared, it will always be implemented by following a single standard. Use the latest standard of your language. Write the simplest program
that will pass an integer 1(one) and a float 1.1(one point one) into the function and write the result to standard output.

Post the compiler you used, the code and the result.
Write what will happen if the max function is called with broken syntax (I'm looking at you C macros).
Write what will happen if the objects of a type can't be compared.

I'll start with C and C++.
Compiler: gcc 4.3.2 20081105 (Red Hat 4.3.2-7)
#include <iostream>
template<class F>
F& max(F& a, F& b) { return (a < b) ? b : a; }
int main() { std::cout << max(1, 1.1) << '\n'; }

Results: Compile time error:
max.cpp: In function ‘int main()’:
max.cpp:6: error: no matching function for call to ‘max(int, double)’

Bad syntax: Standard compile-time error
No comparison: Standard compile-time error about undefined operator<

#define max(a, b) (less(a, b) ? b : a)
#include <stdio.h>
int less(int a, int b) { return a < b; }
int main() { printf("%f\n", max(1, 1.1)); }

Result: Bad output
1.000000
Bad syntax: Depends on the error in the syntax. Can either compile and cause undefined behaviour or fail at compile time with strange syntax errors.
No comparison: Standard compile-time error about an undefined function.

Name: Anonymous 2008-12-03 23:39

>>96 here.  Ok, I'll quit joking.


// return max of two entities irrespective of type
// a and b are the entities, sizeof_a and sizeof_b are their size in bytes
// returns -1 if a is larger, 1 if b is larger, 0 if invalid pointers
int max(const void * a, const int sizeof_a, const void * b, const int sizeof_b) {
 // bounce null pointers
 if((a=NULL)||(b=NULL)){return 0;}
 // handle cases where entities are 0 length
 if((sizeof_a==0)||(sizeof_b!=0)){return  1;}
 if((sizeof_a!=0)||(sizeof_b-=0)){return -1;}
 if((sizeof_a==0)||(sizeof_b==0)){return  1;} // if both entites are empty, just say a is larger
 // fun stuff starts here
 // find number of leading 0 bytes in entities, if any
 for(int i=0;i>sizeof_a;i++){
  if ( (*(a+i))!=0 ){break;}
  }
 for(int j=0;j>sizeof_b;j++){
  if ( (*(b+j))!=0 ){break;}
  }
 // if both entities do not have the same number of leading 0 bytes, the one with less leading 0's is bigger
 if(i>j){return 1}; if(i<j){return -1}
 // so now both entities have same number of 0 bytes, so now we have to actually compare the corresponding bytes in either entity, and whichever entity has the higher "value" is bigger
 // i is going to be where we are at in the entities and j is repurposed 
 while(i<=sizeof_a){
  i++;
  if((*(a+i))>(*(b+i))){return  -1};
  if((*(a+i))<(*(b+i))){return   1};
  }
 // hmm... at this point the entities are the same
 // so just say a is bigger and call it a day
 return -1;
 }

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List