How to find largest and smallest number using WHILE in turboC++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to find largest and smallest number using WHILE in turboC++

2nd Aug 2018, 6:28 AM
Mrinal Sood
Mrinal Sood - avatar
22 Answers
+ 4
Mrinal Sood code is updated, now min & max value are initialized on the first input #include <iostream> using namespace std; int main() { int x = 1, y; // Smallest & largest int min_v, max_v; // Initialization state bool initialized {false}; while (x < 11) { cin >> y; // Break out of the loop if // input failed if(!cin.good()) break; // Initialization check, used // to initialize min_v & max_v // with the first value. if(!initialized) { min_v = max_v = y; initialized = true; } // Check whether y is less // than min_v, if so, // assign y to min_v if(y < min_v) min_v = y; // Check whether y is larger // than max_v, if so, // assign y to max_v if(y > max_v) max_v = y; x++; } cout << "Min value: " << min_v << endl; cout << "Max value: " << max_v; return 0; }
3rd Aug 2018, 9:04 AM
Ipang
+ 4
Good work Ipang I think getting the input in two stages, one outside the while and another inside the loop, eliminates the need for auxiliary flag. #include <iostream> #include <limits> using namespace std; int main() { int x; cin >> x; int min_v, max_v; min_v = max_v = x; while (1) { if (!(cin >> x)) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); break; } if(x < min_v) min_v = x; else if(x > max_v) max_v = x; } //... } Since infinite loop provides the possibility of getting unlimited input, to terminate the process inserting a letter for example, toggles the cin flag to false and breaks the loop.
3rd Aug 2018, 2:24 PM
Babak
Babak - avatar
+ 3
Assuming you're working with an array of int: Declare two int variables, max_v & min_v. Initialize both variables value with array[0]. Setup your while loop to iterate the array, with 'i' as iterator, you can opt to start from index 1. Inside the while loop use two 'if' blocks, each used to get the min_v & max_v. The first 'if' checks whether array[i] < min_v, assign array[i] to min_v if that yields true. The second 'if' checks whether array[i] > max_v, assign array[i] to max_v if that yields true. Code implementation is yours to create, good luck : )
2nd Aug 2018, 8:15 AM
Ipang
+ 3
Mrinal Sood you are going on the right way and got pretty close to it. Here is an edited version of your code with some comments to explain the works. #include <iostream> using namespace std; int main() { int x = 1, y; // These two saves smallest & largest // amongst numbers that were entered int min_v = 0, max_v = 0; while (x < 11) { cin >> y; // Here we check whether y is less // than min_v, we assign y to min_v // if it's true if(y < min_v) min_v = y; // Here we check whether y is larger // than max_v, we assign y to max_v // if its true if(y > max_v) max_v = y; x++; } cout << "Min value: " << min_v << endl; cout << "Max value: " << max_v; return 0; } Hth, cmiiw
2nd Aug 2018, 2:02 PM
Ipang
+ 3
Mrinal Sood The 'if' block inside the loop is the part that decides the smallest & largest value, for example: [First input] min_v = 0, max_v = 0 Input: 4 if(y < min_v) //false, 4 is > 0 min_v = y; // not executed if(y > max_v) //true, 4 is > 0 max_v = y; //input became max_v [Second input] min_v = 0, max_v = 4 Input: -5 if(y < min_v) //true, -5 is < 0 min_v = y; // input became min_v if(y > max_v) //false, -5 is < 4 max_v = y; //not executed And that's how it goes until the while loop ends, when it does, the min_v & max_v will have the smallest & largest value, and we print them on screen. Hope this explains better : )
3rd Aug 2018, 2:24 AM
Ipang
+ 3
Androidus I don't see a necessity there, or a need for using large value, at anytime user input be less than min_v the input value will become the new value of min_v
3rd Aug 2018, 8:05 AM
Ipang
+ 3
Androidus I have updated the code, now min & max are initialized with the first input, this way we can avoid having "out of input list" number for either min or max.
3rd Aug 2018, 9:07 AM
Ipang
+ 2
hinanawi You know you don't have to be mean, right? The reason this app works is because people are kind and helpful to each other, people like you make it hard to keep a community. The guy just asked a question. If you know how to answer, then answer. If not, then don't answer.
2nd Aug 2018, 8:28 AM
Alexandru Turculet
Alexandru Turculet - avatar
+ 2
Thanks a lot Ipang but we havent learnt arrays till now. I'll surely try it when I learn it.
2nd Aug 2018, 8:35 AM
Mrinal Sood
Mrinal Sood - avatar
+ 2
Mrinal Sood Alright, then can you tell how many numbers there is to check for smallest & largest? the use of while loop signified there were many numbers to work with, that's why I thought you were working with array : ) Maybe you can save your code in your SoloLearn profile and post the code URL? it would help to better understand the situation ; )
2nd Aug 2018, 9:22 AM
Ipang
+ 2
Androidus Thanks for reminding me about such possibility of input, inspired by that ...
3rd Aug 2018, 9:51 AM
Ipang
+ 1
hinanawi yeah sure. If you have the solution plz give or dont suggest such things
2nd Aug 2018, 7:16 AM
Mrinal Sood
Mrinal Sood - avatar
+ 1
lpang I want 10 numbers to be checked for smallest and largest Have no idea how to do after this--> https://code.sololearn.com/cqIdOnjZIG37/?ref=app
2nd Aug 2018, 1:42 PM
Mrinal Sood
Mrinal Sood - avatar
+ 1
Ipang THANKS A TON. It worked . But when I assigned min_v and max_v to zero , it only displayed the maximum value correctly. But when I didn't assign any value to the variables(max and min)in the beginning, I got both maximum and minimum values. Also, I don't understand the logic completely. How does the program know that the variables(max_v and min_v) store the maximum and minimum values?(don't judge me plz, I am just a beginner)
2nd Aug 2018, 2:52 PM
Mrinal Sood
Mrinal Sood - avatar
+ 1
Ipang if you want to initialize min and max you should initialize max with 0 and min with a big number like 1000000000
3rd Aug 2018, 7:47 AM
Alexandru Turculet
Alexandru Turculet - avatar
+ 1
Ipang Let's say you have these numbers: 1, 4, 8, 20, 50. If you initialize max with 0, it's gonna be smaller than all the numbers, which is what we want. If we initialize min with 0, then let's see what happens: 1. Is 1 smaller than 0? No 2. Is 4 smaller than 0? No 3. Is 8 smaller than 0? No 4. Is 20 smaller than 0? No 5. Is 50 smaller than 0? No So, in the end, min is still 0. If we initialized min with a big number like 1000000000 then this would be the logic: 1. Is 1 smaller than 1000000000? Yes - min becomes 1 2. Is 4 smaller than 1? No 3. Is 8 smaller than 1? No 4. Is 20 smaller than 1? No 5. Is 50 smaller than 1? No So min becomes 1, which is the smallest number.
3rd Aug 2018, 8:11 AM
Alexandru Turculet
Alexandru Turculet - avatar
+ 1
Ipang guess that works too
3rd Aug 2018, 9:47 AM
Alexandru Turculet
Alexandru Turculet - avatar
+ 1
Ipang thanks I understood the logic now. But ,like Androidus said, if the user doesn't input a value less than 0 , the program won't be able to display the minimum value, which I think happened when I tried initializing min_v with 0. But I haven't learnt the things you used in your updated code(bool etc-as I said I'm just a beginner- so sorry for that) Thanks a lot Ipang and Androidus for helping me so much.
3rd Aug 2018, 1:27 PM
Mrinal Sood
Mrinal Sood - avatar
0
stop using turbo c++ first, then you can talk about doing assignments
2nd Aug 2018, 7:07 AM
hinanawi
hinanawi - avatar
0
our school uses turboc++ only, cant help it
2nd Aug 2018, 7:10 AM
Mrinal Sood
Mrinal Sood - avatar