Results 1 to 7 of 7
Thread: Tell me how this loop works.
- 05-13-2010, 02:00 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 15
- Rep Power
- 0
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?
the whole code is this:Java Code:for (i = 1;i <= max;i++) arr1[i] = arr1[i-1] + i;;
what it prints out: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]);
Any help would be appreciated. Thanks!:)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
- 05-13-2010, 02:39 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 05-13-2010, 04:00 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 15
- Rep Power
- 0
Thanks.
But can you explain how the program runs it? Similar to how I wrote down the steps previously.
- 05-13-2010, 04:30 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I'll try, but the program as a whole doesn't make a lot of sense to me.
You get an int from the user, and save it as max. In your sample run, it was 5.Java Code:System.out.print("Please enter the max number:"); int max = input.nextInt();
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[]arr1 = new int[max+1]; int[]arr2 = new int[max+1]; int[]arr3 = new int[max+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:int i = 1;
1st time through loop: arr1[1] = arr1[0] + 1 = 0 + 1 = 1Java Code:// For-loop to calculate for (i = 1;i <= max;i++) arr1[i] = arr1[i-1] + i;
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
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 = 1;i <= max;i++) arr2[i] = arr2[i-1] + i; for (i = 1;i <= max;i++) arr3[i] = arr3[i-1] + i;
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.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]);
Hope that helps.
-Gary-
- 05-13-2010, 04:38 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 15
- Rep Power
- 0
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
- 05-13-2010, 04:40 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 05-13-2010, 04:50 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
String.contains works in jdk 6, not in 1.4.2
By ScottThornley in forum New To JavaReplies: 1Last Post: 04-16-2010, 03:04 PM -
one works, mine doesn't
By psychop in forum Java AppletsReplies: 0Last Post: 01-25-2010, 11:36 PM -
Custom renderer (almost works)
By geforce2000 in forum AWT / SwingReplies: 11Last Post: 12-13-2009, 09:15 PM -
Anyone know how GroupLayout works?
By ProgrammingPup in forum Advanced JavaReplies: 5Last Post: 12-01-2009, 11:12 PM -
How an array of Threads works...
By Moncleared in forum New To JavaReplies: 6Last Post: 09-09-2009, 01:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks