Results 1 to 7 of 7
  1. #1
    hydride is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Smile Tell me how this loop works.

    So what i have in my head is this:
    Step 1) arr1[i] = arr1[1-1] + 1
    Step 2) arr1[i] = arr1[0] + 1
    Step 3) ???

    what is most troubling is that there is arr1[0] which i dont know how it existed from the above steps. Next is how arr1[0] is 0 when arr1[0] doesn't even exist. Shouldn't it have started out with arr1[1]? Since i = 1?

    Java Code:
    		for (i = 1;i <= max;i++)
    			arr1[i] = arr1[i-1] + i;;
    the whole code is this:
    Java Code:
    System.out.print("Please enter the max number:");
    
    		int max = input.nextInt();
    
    		int[]arr1 = new int[max+1];
    		int[]arr2 = new int[max+1];
    		int[]arr3 = new int[max+1];
    
    		int i = 1;
    
    		// For-loop to calculate 
    		for (i = 1;i <= max;i++)
    			arr1[i] = arr1[i-1] + i;
    		
    		for (i = 1;i <= max;i++)
    			arr2[i] = arr2[i-1] + i;
    		
    		for (i = 1;i <= max;i++)
    			arr3[i] = arr3[i-1] + i;
    		
    		for (i = 0; i <= max; i++)
    			System.out.println("Arr1 " + arr1[i] + " Arr2 " + arr2[i] + " Arr3 " + arr3[i]);
    
    		System.out.println("Sum of All is " + arr1[max]);
    what it prints out:
    Java Code:
    Please enter the max number:5
    Arr1 0 Arr2 0 Arr3 0
    Arr1 1 Arr2 1 Arr3 1
    Arr1 3 Arr2 3 Arr3 3
    Arr1 6 Arr2 6 Arr3 6
    Arr1 10 Arr2 10 Arr3 10
    Arr1 15 Arr2 15 Arr3 15
    Sum of All is 15
    Any help would be appreciated. Thanks!:)

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Java implicitly initializes all uninitialized int variables to 0, including elements of int[] arrays. It's sloppy practice to let the compiler do that, though. It's much better to initialize variables yourself, for the sake of clarity and readability. And arrays always start at index 0. If you create an int[max + 1] array, it will have max + 1 elements (obvious, isn't it?), ranging from 0 to max.

    -Gary-

  3. #3
    hydride is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default

    Thanks.
    But can you explain how the program runs it? Similar to how I wrote down the steps previously.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by hydride View Post
    Thanks.
    But can you explain how the program runs it? Similar to how I wrote down the steps previously.
    I'll try, but the program as a whole doesn't make a lot of sense to me.
    Java Code:
    System.out.print("Please enter the max number:");
    
    		int max = input.nextInt();
    You get an int from the user, and save it as max. In your sample run, it was 5.
    Java Code:
    		int[]arr1 = new int[max+1];
    		int[]arr2 = new int[max+1];
    		int[]arr3 = new int[max+1];
    You create three int[] arrays, all of which go from arrX[0] to arrX[max] (arrX[5] in our sample run). The compiler implicitly initializes all array elements to 0.
    Java Code:
    		int i = 1;
    This line is pretty much useless, except that it saves declaring i in each of the for loops below. The initialization to 1 is completely meaningless, because i is re-initialized in each for loop.
    Java Code:
    		// For-loop to calculate 
    		for (i = 1;i <= max;i++)
    			arr1[i] = arr1[i-1] + i;
    1st time through loop: arr1[1] = arr1[0] + 1 = 0 + 1 = 1
    2nd time through loop: arr1[2] = arr1[1] + 2 = 1 + 2 = 3
    3rd time through loop: arr1[3] = arr1[2] + 3 = 3 + 3 = 6
    4th time through loop: arr1[4] = arr1[3] + 4 = 6 + 4 = 10
    5th time through loop: arr1[5] = arr1[4] + 5 = 10 + 5 = 15
    Java Code:
    		
    		for (i = 1;i <= max;i++)
    			arr2[i] = arr2[i-1] + i;
    		
    		for (i = 1;i <= max;i++)
    			arr3[i] = arr3[i-1] + i;
    You do the exact same thing with the other two arrays, for no reason I can guess, and of course you get the same results in those arrays.
    Java Code:
    	
    		for (i = 0; i <= max; i++)
    			System.out.println("Arr1 " + arr1[i] + " Arr2 " + arr2[i] + " Arr3 " + arr3[i]);
    
    		System.out.println("Sum of All is " + arr1[max]);
    You print out the results, but "Sum of All" seems kind of meaningless -- what you're reporting is just the contents of the last element of the first array.

    Hope that helps.

    -Gary-

  5. #5
    hydride is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default

    awesome explanation!:D
    But when you run it, why does it print out 0 first and then 1, 3, 6...?

    I.E.
    1st time through loop: arr1[1] = arr1[0] + 1 = 0 + 1 = 1
    2nd time through loop: arr1[2] = arr1[1] + 2 = 1 + 2 = 3
    3rd time through loop: arr1[3] = arr1[2] + 3 = 3 + 3 = 6
    4th time through loop: arr1[4] = arr1[3] + 4 = 6 + 4 = 10
    5th time through loop: arr1[5] = arr1[4] + 5 = 10 + 5 = 15

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by hydride View Post
    awesome explanation!:D
    But when you run it, why does it print out 0 first and then 1, 3, 6...?

    I.E.
    1st time through loop: arr1[1] = arr1[0] + 1 = 0 + 1 = 1
    2nd time through loop: arr1[2] = arr1[1] + 2 = 1 + 2 = 3
    3rd time through loop: arr1[3] = arr1[2] + 3 = 3 + 3 = 6
    4th time through loop: arr1[4] = arr1[3] + 4 = 6 + 4 = 10
    5th time through loop: arr1[5] = arr1[4] + 5 = 10 + 5 = 15
    Because your reporting loop starts at i = 0, not at i = 1.

    -Gary-

  7. #7
    hydride is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

Similar Threads

  1. String.contains works in jdk 6, not in 1.4.2
    By ScottThornley in forum New To Java
    Replies: 1
    Last Post: 04-16-2010, 03:04 PM
  2. one works, mine doesn't
    By psychop in forum Java Applets
    Replies: 0
    Last Post: 01-25-2010, 11:36 PM
  3. Custom renderer (almost works)
    By geforce2000 in forum AWT / Swing
    Replies: 11
    Last Post: 12-13-2009, 09:15 PM
  4. Anyone know how GroupLayout works?
    By ProgrammingPup in forum Advanced Java
    Replies: 5
    Last Post: 12-01-2009, 11:12 PM
  5. How an array of Threads works...
    By Moncleared in forum New To Java
    Replies: 6
    Last Post: 09-09-2009, 01:40 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
  •