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.
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.
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