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