Suppose there are some men and you want to find the lightest one. Let’s say that each one of them comes by and tells you his weight. What you must do is, memorize the weight of the first person that has come by and for each new person, you have to compare his weight with the one that you keep memorized. If he is heavier, you ignore his weight. However, if he is lighter, you need to forget the previous weight and memorize the new one. The same procedure continues until all the people have come by.

Let’s ask four men to come by at a random order. Assume that their weights, in order of appearance, are 165, 170, 160, and 180 pounds.

Procedure Value of Variable min in Your Mind!
The first person comes by. He weighs 165 pounds. Keep his weight in your mind (imagine a variable in your mind named min.) min = 165
The second person comes by. He weighs 170 pounds. He does not weigh less than the weight you are keeping in variable min, so you must ignore his weight. Variable min in your mind still contains the value 165. min = 165
The third person comes by. He weighs 160 pounds, which is less than the weight you are keeping in variable min, so you must forget the previous value and keep the value 160 in variable min. min = 160
The fourth person comes by. He weighs 180 pounds. He does not weigh less than the weight you are keeping in variable min, so you must ignore his weight. Variable min still contains the value 160. min = 160

When the procedure finishes, the variable min in your mind contains the weight of the lightest man!

Following are the flowchart and the corresponding program that prompts the user to enter the weight of four people and then finds and displays the lightest weight.

PHP



Java



C++



C#



Visual Basic



Python



Notice: You can find the maximum instead of the minimum value by simply replacing the “less than” with a “greater than” operator in all Boolean expressions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!