3 Replies - 4729 Views - Last Post: 07 February 2011 - 12:33 AM Rate Topic: -----

#1 rfwpresto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-February 11

10x10 2D Array with output 1-100

Posted 07 February 2011 - 12:23 AM

Im trying to make a 2D array that is filled with the values 1 through 100, but i cant seem to figure out how to make it do anything other than 1-10 on each line. I was also having touble getting user input to select a row and print out that specific row, but i hadnt gotten to that point in my code yet.
any help is appreciated!


import java.util.*;
public class OneTroughOneHundred {
public static void main (String[]args){		
int[][] numbers=new int[10][10];		
for(int row=0;row<numbers.length;row++){
for(int col=0;col<numbers[row].length;col++){
numbers[row][col]=1+col;
}
}
for(int row=0;row<numbers.length;row++){
for(int col=0;col<numbers[row].length;col++){
System.out.print(numbers[row][col]+"\t");
}
System.out.println();
}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: 10x10 2D Array with output 1-100

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: 10x10 2D Array with output 1-100

Posted 07 February 2011 - 12:28 AM

you can have a counter x = 1, and assign it to the corresponding index in the 2d array. then increase it for each iteration of the inner loop.
int x = 1;
for(in i = 0; i < numbers.length; i++){
   for(int j = 0; j < numbers[0].length; j++){
       numbers[i][j] = x;
       x++;
   }
}

Was This Post Helpful? 1
  • +
  • -

#3 rfwpresto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-February 11

Re: 10x10 2D Array with output 1-100

Posted 07 February 2011 - 12:32 AM

View Postjapanir, on 07 February 2011 - 12:28 AM, said:

you can have a counter x = 1, and assign it to the corresponding index in the 2d array. then increase it for each iteration of the inner loop.
int x = 1;
for(in i = 0; i < numbers.length; i++){
   for(int j = 0; j < numbers[0].length; j++){
       numbers[i][j] = x;
       x++;
   }
}


so simple. i cant believe i couldnt think of that
thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: 10x10 2D Array with output 1-100

Posted 07 February 2011 - 12:33 AM

As the dude above posted, you should create another counter. Using 1 + col will ensure that you will start again at 1 once the length of the array is finished. Declare another counter other than col outside your for loop and use that variable as the value of numbers[row][col].
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1