Page 1 of 1

## 4 Replies - 801 Views - Last Post: 01 June 2009 - 12:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=107870&amp;s=7a58ef6988dd8ba7c1bce48321a40764&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JackDanielsHere

Reputation: 0
• Posts: 11
• Joined: 31-May 09

Posted 31 May 2009 - 12:51 PM

Can someone help me to make a program in java:
1. Generate 2 rows of 50 random numbers (int) from 0 to 99.
2. Search the numbers that appear in the first row, but not in the second, and numbers that appear in the second row but not in the first. Print the result.
By now I have this:
```import java.util.Random;

public class RandomNum {
int i, j;
int AllNumbers[][] = new int [2][50];
AllNumbers[50] = new int[50];
AllNumbers[50] = new int[50];
for (i=0; i<=50; i++){
for (j=0; j<=50; j++){
AllNumbers[i][j] = RandomNum.nextInt (100);
System.out.println(AllNumbers[i][j] + " ");
}
}
}
}

```

Is This A Good Question/Topic? 0

### #2 ralph_komer

Reputation: 13
• Posts: 71
• Joined: 12-May 09

Posted 31 May 2009 - 12:57 PM

You need to modify the code as below:

```
import java.util.Random;

public class RandomNum {
public static void main(String args[])
{
int i, j;
int AllNumbers[][] = new int [2][50];
for (i=0; i<2; i++){
for (j=0; j<50; j++){
AllNumbers[i][j] = RandomNum.nextInt (100);

}
}
for (i=0; i<2; i++)
for (j=0; j<50; j++)
System.out.println(AllNumbers[i][j] + " ");
}
}

```

Here you are basically initializing the 2 rows of 50 values each with some random numbers and then using another for loop construct to display those values.

Hope it helps.

### #3 JackDanielsHere

Reputation: 0
• Posts: 11
• Joined: 31-May 09

Posted 31 May 2009 - 01:26 PM

Thanks a lot, but I get error with nextInt what should I do to fix it?
And for the search I have only this:
```for(int i=0; i<=50; i++)

for(int j=i+1; j<=50; j++)
if(AllNumbers[i]==AllNumbers[j])
ResultNum[i]=AllNumbers[i];

for(i=0; i<=50; i++)
if(AllNumbers[i]!=ResultNum[i])
System.out.println(""+AllNumbers[i]);

```

Can you help me what I can do to modify this code so that I can make the second part of the assigment?
Thanks again!

This post has been edited by JackDanielsHere: 31 May 2009 - 01:26 PM

### #4 333OnlyHalfEvil

Reputation: 24
• Posts: 664
• Joined: 20-March 09

Posted 31 May 2009 - 03:21 PM

For the second part of the homework, what do you mean display the numbers that appear in the first row but not in the second and vice versa? A two-dimensional array is like a matrix. To do your homework you don't want two dimensional arrays, you want two one-dimensional arrays. Try:
```// first part
Random random = new Random();
int[] FirstRow = new int[50];
int[] SecondRow = new int[50];
for (int i = 0; i < 50; i++) {
FirstRow[i] = random.nextInt(100) + 1;
}
for (int i = 0; i <50; i++) {
SecondRow[i] = random.nextInt(100) + 1;
}
// second part
int count = 0;
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
if (FirstRow[i] != SecondRow[j]) {
count++;
}
if (count == 50) {
System.out.println(FirstRow[i];
}
}
}
count = 0;
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
if (SecondRow[i] != FirstRow[j]) {
count++;
}
if (count == 50) {
System.out.println(SecondRow[i]);
}
}
}

```

Also, make sure you import java.util.*
The random() method returns a random number from 0 to n-1 which is why i add a +1 after it. Hope that helps.

### #5 JackDanielsHere

Reputation: 0
• Posts: 11
• Joined: 31-May 09