Results 1 to 6 of 6
  1. #1
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default 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. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    You used nested for-loops to print the entire array, why don't you use nested for-loops to fill the array.

  3. #3
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default

    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. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    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. #5
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default One minor issue

    Quote Originally Posted by Norm View Post
    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. #6
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default minor issue solved. Nevermind. Thanks again!

    Quote Originally Posted by Norm View Post
    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.

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  2. Help with a loop-like problem
    By Jnoobs in forum New To Java
    Replies: 1
    Last Post: 10-14-2009, 01:15 AM
  3. Replies: 3
    Last Post: 07-23-2009, 07:45 AM
  4. Replies: 1
    Last Post: 07-22-2009, 10:10 PM
  5. Problem - input in two-dimensional array
    By PVL268 in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 04:58 AM

Posting Permissions

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