document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*Problem: Write a program that will find the largest element of an arbitrary array
  2. without using any conditional structures:
  3. banned { if/if-else/switch/for/while/do/?:-operator }
  4.  
  5. bonus: Display the array sorted from smallest to largest with the same conditions.
  6.  
  7. i.e. write the function max in this little program:
  8.  
  9.  
  10. int main()
  11. {
  12.     int array[] = {3,1,4,15,9,2,6,5,35,8,97,93,23,84,62,64,33,83,27,950,28,841,971,69,39,937,510};
  13.     cout << "Largest element is: " << max(array) << endl;
  14.     return 0;
  15. }
  16. 1.If you use STL this is trivial (but far less fun). - not only is it trivial,
  17. but you can quickly come up with 3-5 solutions without even really trying.
  18. Might be fun to see how many different ways we can do this using the standard library.
  19.  
  20. 2.Without using STL the old C library also has functions that can do this easily...
  21. again not nearly as fun, but it is good to realize what is in the library.
  22.  
  23. 3. Lookup tables or jump tables can make effective conditional control structures.
  24. Think about the ?:-operator.
  25.  
  26. 4. http://www.dreamincode.net/forums/topic/43479-the-challenge/
  27. */
  28. #include <iostream>
  29. #include <algorithm>
  30. using namespace std;
  31.  
  32. int main()
  33. {
  34.     int array[] = {3,1,4,15,9,2,6,5,35,8,97,93,23,84,62,64,33,83,27,950,28,841,971,69,39,937,510};
  35.     int size = sizeof(array)/sizeof(int);
  36.     cout << "The array is as follows: " ;
  37.     for (int counter=0;counter<size;counter++)
  38.     {
  39.         cout << array[counter] << " ";
  40.     }
  41.     cout << endl;
  42.     cout << "The largest element is : " << *max_element(array,array+size) << endl;
  43.     sort(array,array+size);
  44.     cout << "The sorted array is : " ;
  45.     for (int counter=0;counter<size;counter++)
  46.     {
  47.         cout << array[counter] << " ";
  48.     }
  49.     cin.get();
  50.     return 0;
  51. }
');