C++ How To Find the Smallest Element in an Array
Find the Smallest Element in an Array
Start with the first element, and keep the smallest value you find:
Example
int numbers[] = {45, 12, 85, 32, 89, 39};
int smallest = numbers[0];
for (int num : numbers) {
if (num < smallest) {
smallest = num;
}
}
cout << "Smallest element: " << smallest;
Explanation:smallest starts as the first element (45).
Each time the loop finds a smaller number, smallest is updated.
Related Pages
C++ How To Find Min and Max in an Array