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);
}
}
}
Random Arraya program that creates a 3 x 5 two-dimensional array with Random
Page 1 of 1
7 Replies - 1217 Views - Last Post: 22 February 2010 - 07:53 PM
Replies To: Random Array
#2
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
you can then use a loop to print your stuff
In the future please
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
This post has been edited by pbl: 22 February 2010 - 07:18 PM
Reason for edit:: Reminder about Code tags
#3
Re: Random Array
Posted 22 February 2010 - 07:15 PM
Why??
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.
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
#4
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
#5
Re: Random Array
Posted 22 February 2010 - 07:17 PM
Also, instead of
you'd want
System.out.println(RA);
you'd want
System.out.println(RA[i][j]);
#6
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
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");
}
}
}
#7
Re: Random Array
Posted 22 February 2010 - 07:25 PM
actually, you can't print an array that way.
you will have to loop that 2d array agian (using 2 for loops) and print each value.
also, look at that code:
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:
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();
}
}
}
#8
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!
Sorry about not posting with the [code] standards. Very helpful!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|