# Thread: 2 Dimensional Loop problem

1. Member
Join Date
Aug 2010
Posts
13
Rep Power
0

## 2 Dimensional Loop problem

I was given the following assignment:

1) Stores the numbers 1 through 50 in a two-dimensional array

2) Use a nested for loop to print the numbers in the 2-dimensional array with 10 numbers per line

So far I did this much coding. I believe I set up the 2-d array and the output. I know how to assign a single value to one cell of the 2-d array (matrix[0][0]=1, matrix[0][1]=2...) but I do not know how to assign a series of numbers(1 to 50) to the 2-d array. I certainly don't want to assign each cell's value individually.

I am in a beginner's Java class. So if someone can offer a simple way to solve this problem, I would be very appreciated. I am sure someone can offer some advance syntax to solve this problem.

Here is the coding I got so far.

public class Array1to50 {

static final int ROWS = 5;
static final int COLUMNS = 10;

public static void main(String[] args)
{
int[][] matrix = new int[ROWS][COLUMNS];

for (int i=0; i<ROWS; i++)
{
for (int j=0; j<COLUMNS; j++)
{
System.out.printf("matrix[%d][%d] = %d\t", i, j, matrix[i][j]);
}
}
}

}

2. Senior Member
Join Date
Aug 2010
Posts
127
Rep Power
0
You used nested for-loops to print the entire array, why don't you use nested for-loops to fill the array.

3. Member
Join Date
Aug 2010
Posts
13
Rep Power
0
I tried the following and the output comes out zero for each cell.

public class Array1to50

{

static final int ROWS = 5;
static final int COLUMNS = 10;

public static void main(String[] args)
{
int[][] matrix = new int[ROWS][COLUMNS];
int cellValue = 0;
for (int i=1; i==50; i++)
matrix[ROWS][COLUMNS] = cellValue;

for (int i=0; i<ROWS; i++)
{
for (int j=0; j<COLUMNS; j++)
{
System.out.printf("%d\t", cellValue);
}
}
}

}

4. output comes out zero for each cell
Java Code:
```int cellValue = 0;
for (int i=1; i==50; i++)
matrix[ROWS][COLUMNS] = cellValue;  // set this element to current value in cellValue (=0)```
Note you go around the loop 0 times but never use i to index the array. The array indexes are always the same. The condition: i == 50 is not true for the first loop. The condition should be i < ROWS*COLUMNS

You need a nest loop to fill the array. The outer loop goes thru the rows from 0 to 4 (5 total) and the inner loop from 0 to 9 (10 total).

The first code you posted shows the correct nested looping for the calls to printf().
Now you need to make another nested loop just like that one to fill the array.
What you assign to each element as you go thru the loops should be a int that you assign a starting value to outside the loops, and then inside the loops you change it to the next value you want it to have. If the next value is one greater than the last one, add 1 to it.
Last edited by Norm; 08-22-2010 at 07:29 PM.

5. Member
Join Date
Aug 2010
Posts
13
Rep Power
0

## One minor issue

Originally Posted by Norm
Java Code:
```int cellValue = 0;
for (int i=1; i==50; i++)
matrix[ROWS][COLUMNS] = cellValue;  // set this element to current value in cellValue (=0)```
Note you go around the loop 0 times but never use i to index the array. The array indexes are always the same. The condition: i == 50 is not true for the first loop. The condition should be i < ROWS*COLUMNS

You need a nest loop to fill the array. The outer loop goes thru the rows from 0 to 4 (5 total) and the inner loop from 0 to 9 (10 total).

The first code you posted shows the correct nested looping for the calls to printf().
Now you need to make another nested loop just like that one to fill the array.
What you assign to each element as you go thru the loops should be a int that you assign a starting value to outside the loops, and then inside the loops you change it to the next value you want it to have. If the next value is one greater than the last one, add 1 to it.

Norm,

Thanks for your help. I got it to work based on your help. One last minor issue. The 1st line of output is not aligned even if I use \t. 2nd to 5th line aligned just fine.

public class Array1to50

{

/**
* @param args the command line arguments
*/
static final int ROWS = 5;
static final int COLUMNS = 10;

public static void main(String[] args)
{
int[][] matrix = new int[ROWS][COLUMNS];

int cellValue = 1;

for (int i=0; i<ROWS; i++)
for (int j=0; j<COLUMNS; j++)
{
matrix[i][j] = cellValue;
cellValue = cellValue + 1;
}

for (int i=0; i<ROWS; i++)

for (int j=0; j<COLUMNS; j++)
{
System.out.printf("%d\t ", matrix[i][j]);

}

}

}

6. Member
Join Date
Aug 2010
Posts
13
Rep Power
0

## minor issue solved. Nevermind. Thanks again!

Originally Posted by Norm
Java Code:
```int cellValue = 0;
for (int i=1; i==50; i++)
matrix[ROWS][COLUMNS] = cellValue;  // set this element to current value in cellValue (=0)```
Note you go around the loop 0 times but never use i to index the array. The array indexes are always the same. The condition: i == 50 is not true for the first loop. The condition should be i < ROWS*COLUMNS

You need a nest loop to fill the array. The outer loop goes thru the rows from 0 to 4 (5 total) and the inner loop from 0 to 9 (10 total).

The first code you posted shows the correct nested looping for the calls to printf().
Now you need to make another nested loop just like that one to fill the array.
What you assign to each element as you go thru the loops should be a int that you assign a starting value to outside the loops, and then inside the loops you change it to the next value you want it to have. If the next value is one greater than the last one, add 1 to it.
Nevermind. I figured out the format flags to use. either %6d or %-6d or whatever the width number you wanted to be. I truly appreciated your help. My first program assignment and I think I spent 3-4 hours on it. Frustrating but I am learning.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•