5 Replies - 10826 Views - Last Post: 25 August 2011 - 01:05 PM Rate Topic: -----

#1 jdl096  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-August 11

Code for Sorting 100 Random Integers into Odd and Even

Posted 25 August 2011 - 11:21 AM

Hey everyone,
I have one more question today that I was hoping everyone would be able to help me with:
I need to write a program that randomly generates 100 integers between 0 and 25. Then (using class methods) I need to sort the odd integers and even integers into separate arrays.

Here is the code I currently have...BUT there are probably errors and it's only about halfway finished!
public class  Unit8Assignment
{
	public static void main(String[] args)
	{
		double[] randomNumbers = new double[100];
		
		for (int i = 0; i < randomNumbers.length; i++) 
		{
			randomNumbers[i] = (int) (Math.random() * 26);
		}
		
		oddEven( random Numbers[i] );
	}

	public static int oddEven( random Numbers[i])
	{
		double[] even;
		double[] odd;
			//Even Numbers
			if ( ( randomNumbers[i] % 2 ) == 1 ) {
				continue; }
			
			randomNumbers[i] = even;
		
			//Odd Numbers
			if ( ( randomNumbers[i] % 2 ) == 0 ) {
				continue; }


			randomNumbers[i] = odd;

		System.out.print( even + " " );
		System.out.print( odd + "  ");

	}
			


	
}
		



Anyone who can help -- it would be GREATLY appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Code for Sorting 100 Random Integers into Odd and Even

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Code for Sorting 100 Random Integers into Odd and Even

Posted 25 August 2011 - 11:22 AM

1. oddEven( random Numbers[i] ); You need to remove the space between the random and Numbers, will not recognize the variable.

2. oddEven( random Numbers[i] ); You don't have that in a for loop and so it will only do the number at i. However, it doesn't even know what that is because i is in the for loop and not in the main method.

3. You need to make the even array and the odd array in the class level.

4. You need to make a for loop for the length of randomNumbers, inside the loop call the oddEven method. Change the method so that it checks it, use an if else statement, if ==0 add it into even array, if ==1 add it into odd array.

5. Make it so that oddEven doesn't return a value.

6. Make 2 separate for loops to cycle through the even and odd arrays printing out each number. Put the for loops in the main method after the loop calling the oddEven method

This post has been edited by Fuzzyness: 25 August 2011 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#3 jdl096  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-August 11

Re: Code for Sorting 100 Random Integers into Odd and Even

Posted 25 August 2011 - 11:42 AM

Still a bit confused...can you show me possibly in the code?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12278
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: Code for Sorting 100 Random Integers into Odd and Even

Posted 25 August 2011 - 11:46 AM

You can only use continue; statements in loops. No real reason for you to have them in your method.

Here is the logic I would use:
-Loop through the original array and count the odd (or even) numbers
-Create two arrays- one to hold odd numbers and the other to hold evens. The sizes will be based on your counter, and the difference between originalArray.length-counter.

-Create two new counter variables starting at 0
-For each element in the array:
  • If it is even, add it to the even array and increment the even counter
  • Otherwise, add it to the odd array and increment the odd counter


We are happy to help you with code you wrote, but we won't simply give you the code.
Was This Post Helpful? 0
  • +
  • -

#5 andyxlee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-August 11

Re: Code for Sorting 100 Random Integers into Odd and Even

Posted 25 August 2011 - 11:48 AM

You should probably sort the array at some point too. Either before you split it into the two arrays or sort both even and odd arrays afterwards.
Was This Post Helpful? 0
  • +
  • -

#6 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Code for Sorting 100 Random Integers into Odd and Even

Posted 25 August 2011 - 01:05 PM

You should use int instead of double for two reasons:
1- you assignment says "write a program that randomly generates 100 integers"
2- the modulo operator (%) is not defined for double (what do you think is 3.1416 % 2 ? and how do you justify your answer ? :))
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1