# Need clarification on Array logic

• 03-03-2014, 04:44 PM
riyazm
Need clarification on Array logic
can anybody explain me the logic behind this array.....I tried a lot but no luck.

how this logic flows

min=max=nums[0];
for(int i=1;i<5;i++){
if(nums[i] < min) min=nums[i];
if(nums[i] > mzx) max=nums[i];
----------------------------------------------------------------------------------------

//Find the minimum and maximum values in an array.
class Minmax{
public static void main(String[] args){
int nums[] = new int[10];
int min, max;

num[0]= 99;
num[1]=-10;
num[2]=1098;
num[3]=234;
num[4]=23;
min=max=nums[0];
for(int i=1;i<5;i++){
if(nums[i] < min) min=nums[i];
if(nums[i] > mzx) max=nums[i];
}
System.out.println("min and max: " +min +" " +max);
}
}
• 03-03-2014, 04:54 PM
gimbal2
Re: Need clarification on Array logic
What about this don't you understand? "The array" is very vague. You don't understand how the application determines the minimum and maximum value? You don't understand how to use an array? You don't understand what an array even is?

Note that the code you posted doesn't even compile, there is no variable "mzx" to speak of. If you want help with code, post the ACTUAL code you have.
• 03-03-2014, 05:00 PM
AlexGraal
Re: Need clarification on Array logic
It's a typo gimbal - it must be "max." A "z" is right next to the "a" on the keyboard...he simply hit the wrong key.

As for his question, he is asking about the small part he referenced at the top, which is this:
Code:

```min=max=nums[0]; for(int i=1;i<5;i++){ if(nums[i] < min) min=nums[i]; if(nums[i] > mzx) max=nums[i]; }```
The idea is simple. You have a list of numbers - lets say, 5,3,8,6,2,9

First, set the min and max to be the first number, 5.

Then, loop through the entire array. Each time, if the number being checked is bigger than the max, set it to max. If it is smaller than the min, set it to min.
So we would check 3 next - 3 is not greater than 5, so max stays 5. It is, however, smaller than 5, so the min becomes 3.
max = 5, min = 3

Then, we check 8. 8 is bigger than 5, not less than 3, so we get
max = 8, min = 3;

Continue on until you get to the end of the list.
And that's it.
• 03-03-2014, 05:05 PM
jim829
Re: Need clarification on Array logic
Quote:

Originally Posted by AlexGraal
It's a typo gimbal - it must be "max."

Do you really think Gimbal didn't know that? Please don't offer solutions to the OP's
when they don't adequately explain their problem. Besides helping folks with code
we try to get them to think and follow the rules (like asking questions intelligently).
That way, they may actually realize their mistake without help.

Regards,
Jim
• 03-03-2014, 05:11 PM
gimbal2
Re: Need clarification on Array logic
Indeed. Typo = "I typed or modified the code", which immediately proves its not the actual code that the OP has a problem with.
• 03-03-2014, 07:34 PM
riyazm
Re: Need clarification on Array logic
Dear All,

Thanks a lot for your kind explanation those responded on my query.
There was a typo error and it my mistake. I am very new to this JAVA technology but not to IT however, I will rectify post my code from next time onwards with ground rules.