Random Array

a program that creates a 3 x 5 two-dimensional array with Random

Page 1 of 1

7 Replies - 1404 Views - Last Post: 22 February 2010 - 07:53 PM Rate Topic: -----

#1 LaMorte85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 10

Random Array

Posted 22 February 2010 - 07:06 PM

Write a program that creates a 3 x 5 two-dimensional array. The program should use loops to populate the array with random numbers between 1 and 100. After the values have populated the array, output the values of the array to the screen in something that looks like a 3 x 5 table.

What I have so far:

import java.util.Random;

public class RA1 {
public static void main(String[] args){
Random num = new Random();
int RA[][] =new int[3][5];

for (int i = 1; i < 100; i++) {
for (int j = 1; j < 100; i++)
RA[i][j] = 1+num.nextInt(100);

System.out.println(RA);
}
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Random Array

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Random Array

Posted 22 February 2010 - 07:14 PM

Your loops should go to cover the array size not the MAX value of your random
and indexes start at 0

for (int i = 0; i < 3; i++) { 
   for (int j = 0; j < 5; j++)
     RA[i][j] = 1+num.nextInt(100);
}



you can then use a loop to print your stuff
for (int i = 0; i < 3; i++) { 
   for (int j = 0; j < 5; j++)
     System.out.print(" " + RA[i][j]);
   System.out.println();
}



In the future please :code:

This post has been edited by pbl: 22 February 2010 - 07:18 PM
Reason for edit:: Reminder about Code tags

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Random Array

Posted 22 February 2010 - 07:15 PM

Why??

int RA[][] =new int[3][5];

for (int i = 1; i < 100; i++) {
for (int j = 1; j < 100; i++)



the array is 3*5... 15 integers.

Also, legal array indexes are zero to n-1 where n is the declared size of the array. By starting ints i and j at one you are skipp the first location in each dimension.

This post has been edited by n8wxs: 22 February 2010 - 07:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 Matteo1988  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 11-February 10

Re: Random Array

Posted 22 February 2010 - 07:15 PM

int RA[][] =new int[3][5];

for (int i = 1; i < 100; i++) {
for (int j = 1; j < 100; i++)

bad, you will get arrayOutOfBoundsException, and also in second loop you increment 'i' variable from outer loop.
Just write:
int RA[][] =new int[3][5];

for (int i = 1; i < 3; i++) {
for (int j = 1; j < 5; j++)
RA[i][j] = 1+num.nextInt(100);

} 

Now all you have to do is make method to print array(
System.out.println(RA) is not correct, you have to write some code ;) )
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Random Array

Posted 22 February 2010 - 07:17 PM

Also, instead of
System.out.println(RA);



you'd want
System.out.println(RA[i][j]);


Was This Post Helpful? 0
  • +
  • -

#6 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Random Array

Posted 22 February 2010 - 07:23 PM

next time you post the code please post it according to the BBCOde rules
and yes
as others have helped
hehe
the web site is too fast
I just took 5 minutes to find the solution and 3 members put the solution
well
here is mine
also read all the post before this one
everything is explained

here is the complete improved code
import java.util.Random;

public class RA1 {
public static void main(String[] args){
Random num = new Random();
int RA[][] =new int[3][5];

for (int i = 0; i < 3; i++)
	{
	for (int j = 0; j < 5; j++)
	{
	RA[i][j] = 1+num.nextInt(100);
	System.out.print(RA[i][j] + "\t");
	}
System.out.println("\n");
}
}
} 

Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Random Array

Posted 22 February 2010 - 07:25 PM

actually, you can't print an array that way.
System.out.println(RA);


you will have to loop that 2d array agian (using 2 for loops) and print each value.

also, look at that code:
for (int i = 1; i < 100; i++) { 
for (int j = 1; j < 100; i++)
RA[i][j] = 1+num.nextInt(100);



you use i and j as your array's indexes. you declare your array to have 3 rows and 5 columns, however you iterate i and j until they reach the value of 100. you can't assign i value bigger than 2 , and you can't assign to j value which is bigger than 4.
as array's indexes are int values between 0 to length - 1.
so start the iteration from i = 0 and j = 0.
pay attention that in your second for loop, you should increase the value of j. not i.

for next time, please post your code between code tags.
this is the fixed code:
import java.util.Random;
public class RA1 {
	
	public static void main(String[] args){
		Random num = new Random();
		int RA[][] = new int[3][5];
		//filling the array
		for (int i = 0; i < 3; i++) { 
			for (int j = 0; j < 5; j++){
		
				RA[i][j] = 1+num.nextInt(100);
		
			}
		}
                 //printing the array
		for (int i = 0; i < 3; i++) { 
			for (int j = 0; j < 5; j++){
				System.out.print(RA[i][j] + "  ");
			}
			//new line
			System.out.println();
		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#8 LaMorte85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 10

Re: Random Array

Posted 22 February 2010 - 07:53 PM

perfect, thanks for the speedy responses as well. I am going to take some time to read through them now.
Sorry about not posting with the [code] standards. Very helpful!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1