### how does min and max program works

how does min and max program works ?

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 #include using namespace std; int main () { int array [5],max=0,low=0,mid=0; for (int w=0 ; w<5; w++) { cout << "Enter value "<> array[w]; } max=array[0]; low=array[0]; mid=array[0]; for (int i=0 ; i<5; i++) { if ( maxarray[i]) { low=array[i]; } } cout << "the minimum value "<<" "<< low<< " "<<"the highest value"<<" "<

first why do we have to do this

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

what the use of it ?

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

 1234567891011 if ( maxarray[i]) { low=array[i]; }

how does this work

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

jana.
Last edited on
Hi there,

This:

 12 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?

 1234567891011 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

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

 1234 if ( max>array[i]) { max=array[i];

How does the program calculate this

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

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

 123456789101112 if ( max

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:

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

the the for loop:

 1234567891011 for (int i=0 ; i<5; i++) { if ( maxarray[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.