# Question about 2D arrays

• 07-22-2009, 02:09 AM
fanle
Question about 2D arrays
Hi guys I wonding if you could possibly help me out with this this problem I have been having.

I have the following 2D array:
Code:

`int[][] processes = {{1,2,3,4,5}, {10,1,2,1,5}, {3,1,3,4,2}};`
What I am trying to accomplish is to repeat the first rows values the number of times stated in the corresponding row of the column below.
For eg.
1111111111233 etc.

Any tips would be greatly appreciated

Thanks
• 07-22-2009, 02:30 AM
Fubarable
What have you tried so far? Please post your best attempt and we can go over it.
• 07-22-2009, 04:06 AM
fanle
Basically so far i have built a for loop and read each of the rows and columns and outputs them. I know in my head how I think it should work but I just cant get my head around the code.

Code:

```int[][] processes = {{1,2,3,4,5}, {10,1,2,1,5}, {3,1,3,4,2}};                         for (int row = 0; row < processes.length; row++)         {                 for (int column = 0; column < processes[row].length; column++)                         {        //if                                 System.out.print(processes[column] + " ");                         }         }```
Where i have typed if I believe there should be an if statement there, however i believe that i need to insert a for loop somewhere else to get the value repeating.
• 07-22-2009, 06:00 AM
Fubarable
This code:
Code:

```    for (int row = 0; row < processes.length; row++) {       for (int column = 0; column < processes[row].length; column++) {         // if         //System.out.print(processes[column] + " ");       }     }```
is almost there.

First, consider the first row of the array to be filled with constants, and the other two rows will tell you how to act on these constants. The first thing you need to figure out is which row to start looping on. My feeling is since you'll need the information from rows 1 and 2 to help tell you what to do with the constants in row 0, that your outer for loop should loop through rows 1 and 2 (the second and third rows), and then loop through every column as your inner loop is doing.

Then you need to figure out what needs to be done inside the inner loop above, where your comments are. Thinking that you'll have to print a number repeatedly -- and every time I think repeatedly, I think of a loop, and since we'll know how many times we need to print each number (it'll be the processes[row][column] value) another inner for loop might fit the bill.

Can you make anything of these suggestions? Please give it a go.
• 07-22-2009, 04:25 PM
fanle
Thanks for the reply made alot of sense when you think of the first row as constants.
I think it is now looping the second row in the outter loop (the third row is now used at this point in the code)
I cant however seem to get the numbers repeating on the printout. I added another inner for loop however i am clearly doing it wrong as it will not repeat :)

Code:

```                int[][] processes = {{1,2,3,4,5}, {10,1,2,1,5}, {3,1,3,4,2}};                                                 for (int row = 1; row < 2; row++)                 {                         for (int column = 0; column < processes[row].length; column++)                         {                                 for (int loop = 0; loop < processes[row][column]; loop++);                                 {                                         System.out.print(processes[0][column]);                                 }                         }                 }```
• 07-22-2009, 05:31 PM
Fubarable
My lord but you are so close.

Your outer-most loop starts fine at index number 1, but ends prematurely. Try looping until process.length not "2" (it should be 3, but better to get in the habit of using array lengths here).

Your inner-most loop is being foiled by a misplaced semicolon. Can you find it? Have you tried to remove it?
• 07-22-2009, 05:38 PM
fanle
:p
Those damn semi-colons.

Code:

```                                for (int loop = 0; loop < processes[row][column]; loop++)                                 {                                         System.out.print(processes[0][column]);                                 }```
:)

Thanks a million for your help
• 07-22-2009, 05:52 PM
Fubarable
You're entirely welcome. It's always nice to help someone who shows a willingness to do some work.