Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Problem: Write a program that will find the largest element of an arbitrary array
- without using any conditional structures:
- banned { if/if-else/switch/for/while/do/?:-operator }
- bonus: Display the array sorted from smallest to largest with the same conditions.
- i.e. write the function max in this little program:
- int main()
- {
- 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};
- cout << "Largest element is: " << max(array) << endl;
- return 0;
- }
- 1.If you use STL this is trivial (but far less fun). - not only is it trivial,
- but you can quickly come up with 3-5 solutions without even really trying.
- Might be fun to see how many different ways we can do this using the standard library.
- 2.Without using STL the old C library also has functions that can do this easily...
- again not nearly as fun, but it is good to realize what is in the library.
- 3. Lookup tables or jump tables can make effective conditional control structures.
- Think about the ?:-operator.
- 4. http://www.dreamincode.net/forums/topic/43479-the-challenge/
- */
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int main()
- {
- 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};
- int size = sizeof(array)/sizeof(int);
- cout << "The array is as follows: " ;
- for (int counter=0;counter<size;counter++)
- {
- cout << array[counter] << " ";
- }
- cout << endl;
- cout << "The largest element is : " << *max_element(array,array+size) << endl;
- sort(array,array+size);
- cout << "The sorted array is : " ;
- for (int counter=0;counter<size;counter++)
- {
- cout << array[counter] << " ";
- }
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement