Results 1 to 7 of 7
  1. #1
    fermon is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Help Understanding this simple code

    Im starting my study on array's using the oracle press book and came across a section that has stumped me. The code below works I'm just not sure how.
    The following line are where I'm having trouble.

    min = max = nums [0];
    if (nums [i] < min ) min = nums [i];
    if (nums [i] > max ) max = nums [i];
    -------------------------------------------------------------------------
    Java Code:
    public class firstarray {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
       int nums [] = new int [10];
         int min, max;
         nums [0] =99;
         nums [1] = -10;
         nums [2] =100123;
         nums [3] =18;
         nums [4] =978;
         nums [5] =5623;
         nums [6] =463;
         nums [7] =-9;
         nums [8] =287;
         nums [9] =49;
         min = max = nums [0];
         for (int i=1; i < 10; i++) {
             if (nums [i] < min ) min = nums [i];
             if (nums [i] > max ) max = nums [i];
             
         }
        System.out.println("min and max:" + min + " " + max);
         
        }
        }
    Last edited by fermon; 11-13-2014 at 02:34 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help Understanding this simple code

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Try playing computer with the code. Use a paper and pencil to write done the values of the variables as they are changed by each statement in the code.

    What do the comments for the code say that the code is supposed to do?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    fermon is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Help Understanding this simple code

    The Code prints the following below which is what its suppose to print, Im just having trouble understanding how it works .
    The three line below are where I'm stuck .

    line 19 min = max = nums [0];
    line 21 if (nums [i] < min ) min = nums [i]
    line 22 if (nums [i] < min ) min = nums [i];

    Code output :
    This is sample [0] : 0
    This is sample [1] : 1
    This is sample [2] : 2
    This is sample [3] : 3
    This is sample [4] : 4
    This is sample [5] : 5
    This is sample [6] : 6
    This is sample [7] : 7
    This is sample [8] : 8
    This is sample [9] : 9

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help Understanding this simple code

    Quote Originally Posted by fermon View Post
    The Code prints the following below which is what its suppose to print, Im just having trouble understanding how it works .
    The three line below are where I'm stuck .

    line 19 min = max = nums [0];
    line 21 if (nums [i] < min ) min = nums [i]
    line 22 if (nums [i] < min ) min = nums [i];

    Code output :
    This is sample [0] : 0
    This is sample [1] : 1
    This is sample [2] : 2
    This is sample [3] : 3
    This is sample [4] : 4
    This is sample [5] : 5
    This is sample [6] : 6
    This is sample [7] : 7
    This is sample [8] : 8
    This is sample [9] : 9
    The code output shown is not produced by the code in your original post.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help Understanding this simple code

    Did you do the paper and pencil exercise with the code? That is a technique you need to learn.
    On paper write down the values in the array.
    Then for this statement:
    min = max = nums [0];
    It assigns the value in nums[0] to the two variables: min and max
    So after that statement
    min = 99
    max = 99

    Now continue through the rest of the code, look at the compares and see if they are true or not
    If true, "execute" the statements inside the if statement by changing the values of min or max as appropriate.
    Increment the value of i for the next iteration through the loop and do it all again.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    fermon is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Help Understanding this simple code

    Sorry the output should be whats seen below .


    min and max:-10 100123

  7. #7
    fermon is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Help Understanding this simple code

    Norm thanks for the response, I guess thats the meaning of pseudocode . It really does help when you write it on paper you're absolutely correct.

Similar Threads

  1. Help in Understanding the code
    By CodeX Pro in forum New To Java
    Replies: 17
    Last Post: 05-19-2013, 07:32 AM
  2. Understanding Problem in Code !
    By Jack_Tauson_Sr in forum New To Java
    Replies: 3
    Last Post: 03-25-2013, 02:20 AM
  3. Problem understanding basic Simple Enumerators
    By eLancaster in forum New To Java
    Replies: 31
    Last Post: 06-23-2011, 06:38 PM
  4. Urgent help understanding code
    By varons666 in forum New To Java
    Replies: 3
    Last Post: 06-03-2011, 07:51 PM
  5. need help understanding part of code
    By mustachMan in forum New To Java
    Replies: 7
    Last Post: 02-09-2011, 07:11 PM

Posting Permissions

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