Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By gimbal2

Thread: Need clarification on Array logic

  1. #1
    riyazm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default 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:
    Java 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.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Need clarification on Array logic

    Quote Originally Posted by AlexGraal View Post
    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
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default 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.
    Norm likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    riyazm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default 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.
    Please provide your valuable suggestions and tips.
    Thanks a lot you all.

Similar Threads

  1. Array list strange logic error, simple fix?
    By monkeyjr97 in forum New To Java
    Replies: 0
    Last Post: 02-05-2013, 12:18 AM
  2. Need a bit of clarification on JNI
    By FlyNn in forum Advanced Java
    Replies: 9
    Last Post: 03-11-2012, 10:38 PM
  3. Logic Error using loop and array
    By janey4115 in forum New To Java
    Replies: 1
    Last Post: 11-17-2011, 06:24 PM
  4. Need clarification..New to java
    By Anveshan in forum New To Java
    Replies: 3
    Last Post: 09-08-2011, 08:55 AM
  5. HELP! need clarification
    By ptsofathrty in forum New To Java
    Replies: 1
    Last Post: 12-16-2008, 08:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •