Skip to content

Instantly share code, notes, and snippets.

@asa55
Created April 3, 2020 22:24
Show Gist options
  • Save asa55/a5b0721040f0ae74d6127faee87caf76 to your computer and use it in GitHub Desktop.
Save asa55/a5b0721040f0ae74d6127faee87caf76 to your computer and use it in GitHub Desktop.
// How do you find the largest and smallest number in an unsorted integer array?
#include <iostream>
#include <vector>
auto main() -> int
{
std::vector<int> unsorted_arr = { 9, 2, 3, 4, 5, 10, 7, 8, 1, 6 };
int* max_index = &unsorted_arr[0];
int* min_index = &unsorted_arr[0];
for ( int i = 0; i < 10; ++i )
if ( unsorted_arr[ i ] > *max_index ) max_index = &unsorted_arr[ i ];
for ( int i = 0; i < 10; ++i )
if ( unsorted_arr[ i ] < *min_index ) min_index = &unsorted_arr[ i ];
std::cout << "MAX: " << *max_index << std::endl;
std::cout << "MIN: " << *min_index << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment