Help!Please!

Help me with 2d array

Page 1 of 1

4 Replies - 610 Views - Last Post: 01 June 2009 - 12:17 PM Rate Topic: -----

#1 JackDanielsHere  Icon User is offline

  • New D.I.C Head

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

Help!Please!

Post icon  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.
Help someone, please?
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] + " ");
			}
		}
	}
}


More help please?

Is This A Good Question/Topic? 0
  • +

Replies To: Help!Please!

#2 ralph_komer  Icon User is offline

  • D.I.C Head

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

Re: Help!Please!

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.
Was This Post Helpful? 0
  • +
  • -

#3 JackDanielsHere  Icon User is offline

  • New D.I.C Head

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

Re: Help!Please!

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

Was This Post Helpful? 0
  • +
  • -

#4 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

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

Re: Help!Please!

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.
Was This Post Helpful? 0
  • +
  • -

#5 JackDanielsHere  Icon User is offline

  • New D.I.C Head

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

Re: Help!Please!

Posted 01 June 2009 - 12:17 PM

Thank you so much!!! Yes the second part is exactly that, when the random numbers are generated, I have to print the numbers from the first row which doesn't repeat in the second and vise versa. Thank you for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1