C++ how to find minimum | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

21st Oct 2021, 4:22 PM
Mando
Mando - avatar
2 Answers
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.
21st Oct 2021, 5:08 PM
Brian
Brian - avatar
23rd Oct 2021, 3:33 PM
AbdElrahman Maans
AbdElrahman Maans - avatar