*
The moose likes Beginning Java and the fly likes help with some homework, help not answers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with some homework, help not answers" Watch "help with some homework, help not answers" New topic
Author

help with some homework, help not answers

Isaac Richards
Greenhorn

Joined: Jul 23, 2012
Posts: 5
i am just starting out using java and am a complete novice to the pgrogrammer world. i am having a hard time getting one of my methods to run correctly and i was wondering if i could get some helpful advice. the question is "Write a method called printGrid that accepts two integers representing a number of rows and columns and prints a grid of integers from 1 to (rows * columns) in column major order. Ii have messed around with some of my calculations but here is what i have so far. the first time i did it i could get the first line of the grid and then the second time i changed stuff and could get it to print the correct row starters, but i cant get them to go together like the output should. any help would be great, thank you in advance.

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

Hi Isaac, welcome to JavaRanch!

First some red tape: please use code tags when posting code samples. It makes the code much easier to read, and it's as simple as clicking the Code button above the edit box. I've already added them for you above.

Now, you first have to be clear about what you're trying to do. I think you're trying to output a grid that looks like:



Is that right? That's tricky because it's much easier to output a row at a time than a column at a time. Why don't you start small and try to output a grid like:



Then you can figure out how to get clever and output it in column-major order. My hint: one loop has to go inside the other. Your "i" loop ends with the closing brace "}", so the braces around your "j" loop effectively do nothing. Also, think about if that 3 really belongs in there, or does that only make things come out right for certain values of row and column?
Isaac Richards
Greenhorn

Joined: Jul 23, 2012
Posts: 5
thanks for the response, i appreciate it. next time i put code in there i will be sure to use the code button. your assumption is correct and i am trying to output a grid (4,6) and am having trouble getting the rows and columns to match up. i can get my rows to number 1 2 3 4, and i can get an ouput of 1 5 9 13 17 21, i just cannot for the life of me get it to go together. i think i am confused on how to start my second line of output starting at two and still going up in increments of four.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

Well, again, you're going to do much better when you get your j-loop actually inside your i-loop instead of just looking like it is. After that you need to find a function of i and j (and maybe row and/or column) that gives the right answer for each position of the grid.
Isaac Richards
Greenhorn

Joined: Jul 23, 2012
Posts: 5
So i nested my j loop, and made some corrections, but am still getting an output that repeats itself here is my new code.


here is my output
1159131721
2159131721
3159131721
4159131721
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Isaac,

Instead of looking at trial and error outputs obtained from running the program, it would be better if you could get hold of a pencil and a paper and write down the values of i and j after each loop iteration. You could make a table consisting of these columns,

1) The value of i
2) The values of j
3) The value at the print statement at line 5.
4) The values of the print statement at line 7.

Just to make stuff clearer, add a space(or any delimiter) at the end of your print statements -> System.out.print(i + " ");

Do let us know your findings.

Isaac Richards
Greenhorn

Joined: Jul 23, 2012
Posts: 5
so after a lot of reading in my text, and retrying my math portion of my code i could still not get it to work. i had to ask my teacher for some help, and she pointed out the obvious fix that i was missing. i needed to use rows *columns in my loop and increase by four. maybe next time i will just re read the question before going insane. thanks for the help guys, i appreciate it.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

Good! I'm glad you got that solved. Maybe you want to post your solution here? I actually can't see how row*columns would be helpful at all, and increasing by four would only work when rows = 4, so it's not a general solution.
Isaac Richards
Greenhorn

Joined: Jul 23, 2012
Posts: 5
there were some other things that i tweaked after we talked about the problem before class. i think i was focused more on one thing that really had nothing to do with it. and with this revised code my print grid worked.

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

Ah, gotcha. Yes, that works, as long as the row count is 4. Change "j += 4" to "j += row", and you'll have a general solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with some homework, help not answers
 
Similar Threads
Nested for loop for grid
Customizing JTable
2-Dimensional Arrays?
spiral traversal of mxn matrix
Java Code