Win a copy of Rust Web Development this week in the Other Languages forum!

Michael Pickens

Greenhorn
since Mar 01, 2012
Cows and Likes
Cows
0
In last 30 days
0
Total given
0
Likes
0
0
Total given
3
Given in last 30 days
0
Scavenger Hunt Ranch Hand Scavenger Hunt Greenhorn Scavenger Hunt

Recent posts by Michael Pickens

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.

9 years ago
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  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.

9 years ago
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 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.
9 years ago
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.

9 years ago
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 []     (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 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

9 years ago
WOO again as I was typing the last reply, I found my answer!!

You guys are awesome. I love hanging out on the ranch!

Thanks again for all your help, sorry I am so frustrating to help!
9 years ago
After thinking all day and jotting stuff down on paper I decided to add the second variable to the method and to create a separate method to print the reversed array.

There is an error that says j cannot be resolved or is not a field.. I don't understand what it means or wants me to do. Here is the code I came up with to swap these indexes.

they way I wrote this out on sever sheets of paper, i hope it works.

Thanks again for all the help. The egg carton example and realizing that i was indeed fixated on writing the code one way really helped. Though I did not know you could put 2 variables into a for loop with 2 sets of parameters, and 2 increments. I am obviously doing it wrong. Can i just separate the two for statements within the same method so I don't get the errors, using the first one to swap the first half of the indexes, and the 2nd one to swap the 2nd half of the indexes?
9 years ago
That is a good question..... I didn't realize that I was just doing that.. sigh.

and now that i look.. the indexes are also reversed..

myArray = 40
myArray = 37
myArray = 62
myArray = 60
myArray = 3
myArray = 79
myArray = 43
myArray = 65
After reversing the array
myArray  = 65
myArray  = 43
myArray  = 79
myArray  = 3
myArray  = 60
myArray  = 62
myArray  = 37
myArray  = 40

I dont know what to do. Pen and paper "playing computer" doesn't make sense to me. When I do it, i pretend that my array size is 5 and trace it through. I see that when I swap, that I end up swapping back with the third swap... I do not understand how to fix the problem. I have also made it so the last numbers will reverse, but it is either the first half of numbers or the 2nd half of numbers. I tried writing 2 methods.. one that would reverse the fist half and one that would reverse the 2nd half.. but of course, as you guessed, didn't compile and I did not no how to fix that. We have not studied objects as of yet.. that is the last chapter we are doing this semester. I have a mid term tomorrow in this class on loops, methods, and arrays (to give you an idea of where I am at.)
9 years ago
Thanks for all your help. I didnt turn on my computer, read the post on my phone, and started jamming on paper. Then it occurred to me.. why am I swapping, why don't i Just reverse it through a for loop..

I want to rewrite this later with a separate final print method, but I could not get it to work right.

here is my final snippet of code.

I appreciate the help, some of the help was above my understanding currently, but at least it gives me something to think about..

Cheers.

9 years ago
I changed the code to try to adjust the length. I see from doing a series of println statements .. that I am only switching the first and last statements, then switching them back.

What started out for me as a logical process has now turned into a big guessing game . when i added the /2 to the length, it only prints out like this:
myArray = 36
myArray = 91
myArray = 56
myArray = 22
myArray = 14
myArray = 37
myArray = 49
After reversing the array
myArray = 49
myArray = 37
myArray = 14

thus you can see I am missing 4 out of 7 indexes.. i suppose that is because arrayOne.length/2 = 3 (7/2=3).
9 years ago
I changed that code to match:

9 years ago
I have run through this with pencil and paper several times. I am not catching the error. I thought that since I really only need to go through half of the array that I could do arrayOne.length / 2, but that of course only gives me half of the array, but chops off the non reversed section.

Thanks.
9 years ago
I am working on yet another homework assignment. This assignment is asking me to write a program which creates an array with its size determined by users and filled with random numbers between 1 and 100.

I must obtain the number of random numbers from the user through a GUI dialog box.
The 2 guidelines that my professor is requiring are:
1) My program should have at least two methods, "displayArray" and "reverse"
2) I am not allowed to create a new array within the method "reverse" to perform the task.

I have this code completely written, and I am having trouble reversing ALL the digits.

Output of my program: 7 random digits
myArray = 5
myArray = 14
myArray = 88
myArray = 18
myArray = 20
myArray = 8
myArray = 46
myArray = 100
After reversing the array
myArray = 100
myArray = 46
myArray = 8
myArray = 20
myArray = 20
myArray = 8
myArray = 46
myArray = 46

And since I am not allowed to simply make a new array, the code posted below is how I am handling the reverse.

Thanks again for the help.
9 years ago
I got it.. as I typed my last reply... bad mistake on my part.

thanks
9 years ago