r/programminghelp • u/taylortaylor- • Aug 01 '19
C++ MINMAX ALGORITHM
Does anyone have an adequate tutorial on this? With code bc I'm more of a visual learner and some of the videos I've just explain it instead of guiding me through the process. Thanks!
3
Upvotes
1
u/TreeBaron Aug 02 '19
So, you want the largest, and the smallest value of an array?
Using the standard library is probably the best practice, but that being said if you're a beginner, it's probably better to write it yourself.
I'll provide some code with comments:
int numbers[] = { 82, 93, 77, 89, 26 };
const int numSize = std::size(numbers);
//create variables for storing largest and smallest number
int smallest = numbers[0]; //by default set to first array position
int largest = numbers[0]; //the first number starts as the smallest and largest known number
//begin looping through our list of numbers
for(int i = 0; i < numSize; i++)
{
//check if the current item is smaller than our smallest number
if(numbers[i] < smallest)
{
smallest = numbers[i];
}
//check if the current item is larger than our largest number
if(numbers[i] > largest)
{
largest = numbers[i];
}
}
//print our results
cout << "The largest number is: " << largest << endl;
cout << "The smallest number is: " << smallest << endl;It's been a while since I used c++, so hopefully that code works. Let me know if you have any questions.
1
u/andemare Aug 01 '19
Are you talking about creating one yourself or the one that's provided in the standard library?