Read 20 numbers and determine the highest and lowest This exercise if very interesting, and has some tricks to work. RRecuerda
in programming in general, whether in
C, C + +, Pascal, HTML, Java, PHP and other , the logic
is an indispensable factor.
The title of the year are as follows:
Read 20 numbers and find the highest and lowest value of the readings.
For this exercise, I use a
CYCLE FOR to read the 20 data that the user must enter, and within the same cycle,
ANIDARÉ a couple of conditions to determine the highest and lowest number of values \u200b\u200bthat are entered.
# include \u0026lt;iostream.h>
int i, major, minor, a;
int main (void )
{court \u0026lt;\u0026lt;"Welcome \\ nPlease enter 20 numbers and the program will determine the highest and lowest "\u0026lt;\u0026lt;endl \u0026lt;\u0026lt;endl;
minor = 99999999;
higher = -999;
for (a = 1, a \u0026lt;= 20; a + +) {
court \u0026lt;\u0026lt;"Enter number" \u0026lt;\u0026lt;endl;
cin>> i;
if (i> largest) {
largest = i;}
if (i \u0026lt;low) {
minor = i;}
} court \u0026lt;\u0026lt;"The largest number entered is" \u0026lt;\u0026lt;largest \u0026lt;\u0026lt;endl;
court \u0026lt;\u0026lt;"The smaller number entered is "\u0026lt;\u0026lt;lowest \u0026lt;\u0026lt;endl;
system (" pause ");
return 0;}
This exercise can be done certainly prettier, but hey, here's the trick to work:
►
At first, I declare the major and minor numbers
with presets, the largest
, a very "small" within the set of real numbers, and the lowest
, on the contrary, a very " large "; all this, so that with each iteration of the loop, assuming that the user does not enter exorbitant figures, both values \u200b\u200bare correctly allocated the largest and the child. year