• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Nested for loop for grid

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 10889
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
infact that was a terrible idea...sorry.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
BWA HA HA HA HA HA HA! Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic