0

How do we find the lowest?

For example int x[4] = { 3, 9, 7 , 10} How do I find which is the smallest number? This is regarding the ticket office question in c++ .

17th Apr 2021, 5:16 AM
Kruti
Kruti - avatar
5 Respuestas
+ 6
Kruti Store first value in a variable and compare with next value. If next value is less than 1st value then that would be smallest number otherwise 1st value will be smallest number. int x[4] = {3, 9, 7, 10}; int min = x[0]; for (int i = 0; i < 4; i++) { if (x[i] < min) min = x[i]; } cout << min;
17th Apr 2021, 5:21 AM
A͢J
A͢J - avatar
+ 2
Kruti Doesn't matter on order just try shared logic.
17th Apr 2021, 9:16 AM
A͢J
A͢J - avatar
0
And what if the values are not in increasing order? For example int x[4] = { 12, 7, 20 , 1} Like when four numbers are randomly selected by the user?
17th Apr 2021, 7:48 AM
Kruti
Kruti - avatar
0
Kruti if values are not in sorting or then u can use any sorting techniques to sort the values u can easily find min max or u can use STL library std::min() , std::max()
17th Apr 2021, 11:20 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
#include <iostream> using namespace std; int main() { int k[5] = {7, 2, 8, 1, 4}; int min = *k; for (int i=0;i<5;i++) { if (k[i] < min) min = k[i]; } cout << min; return 0; } // Hope this helps
18th Apr 2021, 4:19 PM
Calvin Thomas
Calvin Thomas - avatar