+ 1
C++ how to find minimum
include <iostream> using namespace std; int main() { int ages[5]; int minimum = ages[0]; int maximum = ages[0]; int i = 0; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } if (minimum<ages[i]){ minimum = ages[i]; } if (maximum<ages[i]){ maximum = ages[i]; } cout << minimum << endl; return 0; } Why won’t it find the minimum
3 odpowiedzi
0
Your minimum and maximum variables are initialized to unpredictable values.
You need to initialize them after the ages have been input.
Your logic to compare minimum is wrong. Use > instead of <.
You need to put the comparisons inside a loop.