• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help tracing an embedded for loop in an array  RSS feed

 
Michael Pickens
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering if you guys could help me trace some loops.

This was a past in class exercise, and I am only studying it to help me for the final this Friday.

list [] [0] [1] [2] [3] (6, 4, 5, 3 respectively are listed as the index values of the array - thus the array length is 4.).




I have these spaced weird so I can insert print statements between each line, but it is not helping - basically it is printing out k.. or when I have it print a specific index, it prints out 6 of them, which I am not sure why unless it is taking the value of list[0] as the number of times to execute the loop?

I am confused at which value is going into the loop and what each loop controls exactly. My thought is that the first loop controls the number of times that the loop will run and if the conditions are true for the first loop, then it executes the second loop which controls how many times it swaps the indices? I am not even sure if this is something that can be helped with over the forums page.

This is asking that I trace the program and give the contents of list [] when: k = 1, k = 2, and k = 3. I know that arrays pass by reference

Thanks in advance for your assistance, it's always appreciated.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Pickens wrote:
list [] [0] [1] [2] [3] (6, 4, 5, 3 respectively are listed as the index values of the array - thus the array length is 4.).


Huh? Is that a 4D array, with the first dimension unspecified? Or are you listing the indices as 0, 1, 2, 3? What are 6, 5, 4, 3?





I have these spaced weird so I can insert print statements between each line, but it is not helping - basically it is printing out k..


Well, yeah, because that's what you're telling it to print out.

or when I have it print a specific index, it prints out 6 of them


Not clear what you mean here, but but during one pass of the outer loop you'll get multiple passes through the inner loop, and of course the outer loop's index doesn't change during that time.

If you can provide an SSCCE, and show the exact output, and explain more clearly what the problem is or what you expected instead, it will be a lot easier to understand than these partial snippets and vague explanations.
 
Michael Pickens
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies, I was really trying to better explain this. I do not have a compilable version.. but I typed in the rest of the stuff so it would compile... (this was only an in-class worksheet and was not written in full code)

this is a single dimension array and 6, 4, 5, 3 are just the values in the indices.



so when k = 2, that means the outer loop executes twice, and the inner loops executes itself 3 times for each execution of the k loop? thus the i loop executes itself 6 times?

I have the solutions, and if I execute the "i" for loop 3 times it gives me the correct answer for k = 1.

Forgive me, I am trying hard to explain this the best I can, and this is the best SSCCE i can provide.

I do not need to know what k is equal to.. i am only trying to figure out what is in the array once the program is run, based on if k is equal to 1, 2, or 3.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Pickens wrote:


What do you think that compares? When i is 0 and list[i] is 6, what is the value of list[i] + 1?
 
Michael Pickens
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please forgive me, I have been messing around with this code for about 3 hours - I want to understand this perfectly so I am being very thorough.

In messing around with my code, I changed the line you are talking about, my apologies. It should read



Thus list [i+1] if i = 0 is list[1] with the index value of 4.

I have made some progress, but I am not sure I understand it. When I run the inner loop 3 times, it gives me the correct array index contents (values) for list [] for k = 1. when i trace it through a fourth time, it gives me the correct array index values for k = 2. Then, when I run it through for a fifth time, it again gives me the correct values for k = 3. Where I am confused, it says in our text book that "Each time the outer loop is repeated, the inner loops are reentered, and started anew". And Jeff said earlier that each time the outer loop runs, the inner loop will run multiple times. So, I would run the inner loop until i = list.length - k, then go back out and run the outer loop again, and repeat the process until k < list.length is false?

Again, sorry for my input error.

And thanks again for help.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it supposed to be a bubble sort?
I suggest you remove that swapping code from that method and put it into a utility class. Search this forum for utility class and you find they have one private constructor only (so you can’t create an instance) and all their members are marked static. Examples: 1 2 3 Note those examples probably have different methods from what you want. You want a swapTwoValuesInArray(int[] array, int i, int j) method, or something like that. Once it is in a utility class, you can reuse it.
 
Michael Pickens
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am only supposed to indicate what is in the array list [] after each iteration of the outer loop.

when k = 1, the array contents are list [] {4, 5, 3, 6}

when k = 2, the answer on the sheet is list [] {4, 3, 5, 6}, but when I trace the inner loop from 0 to 1 twice, my results for the array are list [] {5, 3, 4, 6}. I do not understand how 4 stays in index [0] if you are swapping 0 to 1 twice for k = 2.

I am starting to think the solutions that were given to me are wrong.

Thanks for your help everyone!
 
Michael Pickens
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl and everyone, thanks for your help, I got it figured out. You were right, I was overlooking the if statement and the values of it. I thought that if the first iteration of the loop was false, that it would just exit, but now I understand that the line i < list.length-k actually protects the loop from exiting in case of a false statement.

Again, thank you everyone for your help. I think this is the last programming class for me, and ya'll were here to help me whenever I needed. Much appreciated.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!