how does min and max program works

how does min and max program works ?










1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;


int main ()
{




    int array [5],max=0,low=0,mid=0;

    for (int w=0 ; w<5; w++)
    {
        cout << "Enter value "<<endl;

        cin >>    array[w];

    }

    max=array[0];
    low=array[0];
    mid=array[0];

    for (int i=0 ; i<5; i++)
    {







        if ( max<array[i])
        {

            max=array[i];
        }


        else if ( low>array[i])
        {
            low=array[i];
}
}
    cout << "the minimum value "<<"  "<< low<< "  "<<"the highest value"<<"  "<<max<<endl;


}





first why do we have to do this

1
2
 max=array[0];
    low=array[0];


what the use of it ?



----------------------


1
2
3
4
5
6
7
8
9
10
11
  if ( max<array[i])
        {

            max=array[i];
        }


        else if ( low>array[i])
        {
            low=array[i];
}



how does this work

how could the program know the maximum and the minimum number from this simple structure ??!!


please tall me how

jana.
Last edited on
closed account (o3hC5Di1)
Hi there,

This:

1
2
max=array[0];
low=array[0];


Sets the minimum and maximum to the value of the very first element of the array.
You could set it to any other array value if you like, but the first one is convenient.
This just serves as to have a basic value to compare against, if we didn't do this, how would we check if a value in the array is bigger?

1
2
3
4
5
6
7
8
9
10
11
if ( max>array[i])  //if the current maximum is smaller than the value at array[i]
        {

            max=array[i];  //make that value the new maximum
        }


        else if ( low<array[i]) //similarly, if current minimum is smaller than the value array[i]
        {
            low=array[i]; // make it the new minimum value
}


Note that I switched the <> operators in the if-statements, they are the wrong way in the code you presented.

So you compare a value against all the elements in the array, if an element is bigger/smaller, that becomes the new maximum/minimum. At the end of the array, the value in max and low will be the highest and lowest number in the array.

Hope that helps.

All the best,
NwN
thank very much it was helpful for more understanding , but my question is how the operator works (calculation)

like

if the input were
1
2
3
4
5

how does the program figure out the min and the low value

from this


1
2
3
4
if ( max>array[i])  
        {

            max=array[i];


How does the program calculate this



-----------------------------

I tried to switch the <> operators in the if statements


1
2
3
4
5
6
7
8
9
10
11
12

        if ( max<array[i])
        {

            max=array[i];
        }


        else if ( low<array[i])
        {
            low=array[i];
}


and it works fine

the <> operators is similar

now how does the program know the min and the max number , since the <> operators is similar ( in the same direction)




This is it:

First let's assume these are the user inputs in the array:
Index Value
0 2
1 3
3 7
4 1
5 9
then it assigns 2 to the variables max,mid & low because, it assigns the value contained by the first element in the array to max & mid & low:

1
2
max=array[0];
low=array[0];


the the for loop:

1
2
3
4
5
6
7
8
9
10
11
for (int i=0 ; i<5; i++)
    {
        if ( max<array[i])
        {
            max=array[i];
        }
        else if ( low>array[i])
        {
            low=array[i];
        }
}


first, this for loop, declares and initializes an int i to 0: int i=0 then if i<5 the loop will work!!! because that is the condition, and it is met because i=0 also i++ means i should increase by 1, any time the loop fully works!

in the loop, there is an if statement that checks if the current value in max(which is 2 in my example) is lesser than the value in the first element of the array.

if(max<array[i]) //here i's value is 0 on the first run of the loop and is equivalent to array[0]

and it is not so it does not meet the condition and therefore moves on to the else if statement, which works just like the first if statement.

then when it finishes, it increases i by 1 and checks the condition again, which is eventually met.

So it goes to the if, this time i=1 and not 0

so since array[1] or array[1]=3, max would be given the value array[1] contains, which is 3.

It does this until the for loop eventually ends.
Then it displays max and min, which would eventually be 9 and 1 respectively!!! computer stuff !!!LOL!!!
Last edited on
Now I've understood how it works , it would take long time it know these myself.

Thank you very much for the great explanation , I appreciate that .

I understood every thing from this line ^^ (then when it finishes, it increases i by 1 and checks the condition again, which is eventually met.)

it was really helpful , thanks.
If you would like ...

#define MAX(a, b) f((a) > (b) ? (a) : (b))

#define MIN(a, b) f((a) < (b) ? (a) : (b))

And now use them in for loop to find out the needed one. But I doubt this is not exactly what you need. ;-) , but hope still it will help you...
Topic archived. No new replies allowed.