my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Nested for loop for grid Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested for loop for grid" Watch "Nested for loop for grid" New topic
Author

Nested for loop for grid

Michael J Brown
Greenhorn

Joined: Mar 02, 2011
Posts: 2
Hi all,

I'm in a beginning Java class (CS Major), and we're working with nested for loops. I sort of have the concept down, but am stuck on this particular problem. Before I continue, I just want to say that if you are going to help me out, don't just link me to a tutorial and ask me to figure it out (I wouldn't be asking anyone, if I ended up finding the solution that way). I don't expect anyone to give me the answer straight up, but just give me a hint that will help me figure out what I am doing wrong and how to do it correctly. Here's the problem:

Write a method that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows in the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows * cols). The output is displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right, once the bottom of the current column is reached. Assume that the rows and cols are greater than zero.

Call: printGrid(3, 6);

1, 4, 7, 10, 13, 16
2, 5, 8, 11, 14, 17
3, 6, 9, 12, 15, 18

Here's my pseudocode:

For numbers (i) going from one to 18 (i <= (rows * cols)), print numbers 1, 2, 3, 4,.... 18 (i++)
Print rows one to 3, then add 3 to the following column on each row, and so on. There seems to be a pattern where the numbers each column after the first one increase by 3 on each row. (I can't seem to figure out how to algebraically fit this into the code, so I think that might be the problem).

NOTE: We haven't started using arrays, so this is just a nested for loops exercise. I have no ideas how arrays work (I've seen some sites use it), so please don't help me out by using those.

Here's my code:


I plug that code into JGrasp (the program we use in class), and I get "123123123123123123123123123123123123123123123123123123". 123 gets repeated 18 times, which isn't right.

What am I doing wrong here? Thanks in advance for the help.

-Mike

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Mike

Welcome to JavaRanch!

"Nested" loops implies more than one loop; you're trying to create a solution in one loop, which is possible, but actually a little harder.

Try something like this:



For the starred line, you have to figure out the formula. It's not hard!


[Jess in Action][AskingGoodQuestions]
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 174

A couple of hints:
Inner loop should be cols, outer loop should be rows.
After inner loop you need a new-line.
You'll need a simple formula in the print statement to compute value.
Michael J Brown
Greenhorn

Joined: Mar 02, 2011
Posts: 2
Ernest and Carey:

Thanks for the quick responses. I'll give it a go once again with the suggestions given, and respond with my results shortly!

Thank you again!

-Mike
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

I would also suggest breaking it down. Don't try and get it all to work in one go, but just do a piece at a time.

I, personally would start with either trying to print every element correctly in ONE row, or trying to print the first element only from each row.

Let's try printing the first element of each row only. That seems pretty straight forward - a simple loop spanning the correct number, and a counter.

once you have that working, you would want to figure out how to print the full row instead of a single number. If you look at your data, can you figure out how, assuming you know the first data element, how to compute all the others, and how many you will need on each row?

Once you do that, you can replace the single line (that only prints the first element) with a bunch of lines that print every element of the row...

My point it that it is much easier, and almost always much better, to try and break your problem down into little pieces. recompile and test FREQUENTLY - I tend to re-compile after every 2-3 (at most) new lines are written.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

Maybe it would make your life easier if you used another variable to hold the value you want to print out instead of trying to work it from i and j, just a thought anyway.


You're on a gravy train with biscuit wheels Roy.
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

infact that was a terrible idea...sorry.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Ernie Mcracken wrote:infact that was a terrible idea...sorry.


No, that's actually a fine way to do it, too. After Michael works out his solution, I can explain why.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested for loop for grid