Array/Random Help

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 744 Views - Last Post: 08 February 2011 - 01:40 PM Rate Topic: -----

#1 underoath23  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 22
  • Joined: 05-December 10

Array/Random Help

Posted 07 February 2011 - 09:06 AM

Hey all,
I need some help writing a small program. I want to be able to make an array of 10 int's, which are generated randomly. I want to to be inside of a for loop. Thats a major part im stumped on. I grabbed some code and added a little to it of a 7 number generator but its not working either. I am fairly new to java, and really new to array's in java. So any help is greatly appreciated. Sorry If I seem like a 'noob', but you never know if you don't ask. Thanks!

package project2Pak;

import java.util.*;

public class ProjectTwo
{
	public static void main(String[] args)
	{
		Random randNumGenerator = new Random();
 
		int[] x = new int[7];
		for (int i=0; i<x.length; i++)
		{
			x[i] = (randNumGenerator.nextInt(100)+1);
		}
	}
}
class myRanNumbers 
{
  private int[] numbers;

  public myRanNumbers (int n )
  {
    numbers = new int[n];

    // initialise numbers
    int i = 0;

    while (i < n ) 
    {
      int r = (int) Math.floor(Math.random() * 100);
      if (add (numbers, i, r)) 
      {
        ++i;
      }
    }

   show_all (numbers);
 }

 private boolean add (int [] list, int size, int val)
 {
   for (int i = 0; i < size; i++ )
   {
     if (list[i] == val) 
     {
       return false;
     }
   }
 list[size] = val;

 return true;
 }

 private void show_all (int[] list)
 {
   for (int i = 0; i < list.length; i++ ) 
   {
   System.out.print(list[i] + " ");
   }
   System.out.println();
   }
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Array/Random Help

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Array/Random Help

Posted 07 February 2011 - 09:12 AM

What exactly is the problem? Do you get any errors? if yes, post the error messages.
Or, Do you get an unexpected output? what is the desired output then?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10564
  • View blog
  • Posts: 39,091
  • Joined: 27-December 08

Re: Array/Random Help

Posted 07 February 2011 - 09:15 AM

Why do you need the myRanNumbers class? Just change the size of the array in the main() method from 7 to 10.
Was This Post Helpful? 1
  • +
  • -

#4 underoath23  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 22
  • Joined: 05-December 10

Re: Array/Random Help

Posted 07 February 2011 - 09:17 AM

View Postjapanir, on 07 February 2011 - 09:12 AM, said:

What exactly is the problem? Do you get any errors? if yes, post the error messages.
Or, Do you get an unexpected output? what is the desired output then?



Well, it runs with no errors, but It doesn't print anything to the screen. Eventually I will want four separate outputs doing different things, I am pretty sure I can get that to work when I get there. But I need this to be able to out put the 10 random numbers.

Also the extra class I deleted. It was part of the other code a mentioned.
Was This Post Helpful? 0
  • +
  • -

#5 underoath23  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 22
  • Joined: 05-December 10

Re: Array/Random Help

Posted 07 February 2011 - 09:23 AM

Here is an updated code, but it does not print out integers, just random characters.
package project2Pak;

import java.util.*;

public class ProjectTwo
{
	public static void main(String[] args)
	{
		Random randNumGenerator = new Random();
 
		int[] nums = new int[10];
		for (int i=0; i<nums.length; i++)
		{
			nums[i] = (randNumGenerator.nextInt(100)+1);
		}
		System.out.println("The Random numbers are " + nums);
	}
}
 

Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Array/Random Help

Posted 07 February 2011 - 09:28 AM

A simle program to assign 10 random values to an array and then print the array would be:
import java.util.Random;

public class A {

    public static void main(String[] args) {
    	Random gen = new Random();
    	int[] arr = new int[10];
    	for(int i = 0; i < arr.length; i++){
    		arr[i] = gen.nextInt(100) + 1;
    	}
    	for(int i = 0; i < arr.length; i++){
    		System.out.print(arr[i] + ", ");
    	}
   }

}

To print on the screen, use a print statement like System.out.println or System.out.print

If you want to print an Array, you can't just do:
System.out.println("The Random numbers are " + nums);

You'll either have to iterate the array and print any value individually (like in the code i posted).
or, you can invoke the toString method of Arrays class, and print it like:
System.out.println(Arrays.toString(nums));

If you choose to call Arrays.toString, dont forget to import Arrays:
import java.util.Arrays;

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10564
  • View blog
  • Posts: 39,091
  • Joined: 27-December 08

Re: Array/Random Help

Posted 07 February 2011 - 09:29 AM

Why use two loops when you can use one:
for(int i = 0; i < nums.length; i++){
    nums[i] =  (randNumGenerator.nextInt(100)+1); 
    System.out.println(nums[i]);
}


Was This Post Helpful? 1
  • +
  • -

#8 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Array/Random Help

Posted 07 February 2011 - 09:36 AM

Quote

Why use two loops when you can use one:

Just to clarify on that. In my example I wanted to demonstrate how to fill an array with random numbers, and then print that array.
That would be useful if you want to create an Array, and print it only later in your code.
However, if you want to fill the array and print the values immediately, better do it in one loop as macosxnerd101 showed. :)
Was This Post Helpful? 1
  • +
  • -

#9 underoath23  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 22
  • Joined: 05-December 10

Re: Array/Random Help

Posted 07 February 2011 - 09:55 AM

ok that helps. Im guessing now I should do this all in one loop. But here is my goals. 4 lines out output that, prints the array values, next print all the even numbers in the array, (by the way I changed my domain to 1-10 instead of 100), next the values of the array in reverse order, and finally the first value and last of the array. on four seperate lines. This project was a practice problem in my java book, but it did not provide a guide. So thats why my requests are so specific haha. Thanks again for all the help thus far!
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10564
  • View blog
  • Posts: 39,091
  • Joined: 27-December 08

Re: Array/Random Help

Posted 07 February 2011 - 10:02 AM

1) To reverse the array, loop from the array length-1 to 0.
2) Printing the first and last values of the array should be easy. You should easily know how to do this.

3)Printing out even numbers comes down to a loop and an if statement. Hint- how do you check is a number is even using the % modulus operator?
Was This Post Helpful? 2
  • +
  • -

#11 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Array/Random Help

Posted 07 February 2011 - 10:02 AM

Here are some hints:
an even number is a number that has no remainder when divided by 0. use the modulo operator '%'.

As you used a loop to print the array, the first value was the value in index 0 in the array, and the last was the value in index array.length - 1.
Print the array reversed by iterating from index = array.length and ending at index = 0.

Ninjad by macosxnerd101 :P

This post has been edited by japanir: 07 February 2011 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#12 underoath23  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 22
  • Joined: 05-December 10

Re: Array/Random Help

Posted 07 February 2011 - 10:09 AM

View Postjapanir, on 07 February 2011 - 10:02 AM, said:

Here are some hints:
an even number is a number that has no remainder when divided by 0. use the modulo operator '%'.

As you used a loop to print the array, the first value was the value in index 0 in the array, and the last was the value in index array.length - 1.
Print the array reversed by iterating from index = array.length and ending at index = 0.

Ninjad by macosxnerd101 :P

I understand how to set up all these criteria, I was just making sure I could do it all in one loop, or do I need to declare the random numbers globally outside of a loop at all
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10564
  • View blog
  • Posts: 39,091
  • Joined: 27-December 08

Re: Array/Random Help

Posted 07 February 2011 - 10:13 AM

You will need to use separate loops.
Was This Post Helpful? 0
  • +
  • -

#14 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Array/Random Help

Posted 07 February 2011 - 10:14 AM

No. first fill the array with random values, and then perform all these actions.
Actually, you can do it inside one loop, but then the assignment loses it's point (The assignment want you to work on arrays, loop backwards, forward. access values by their corresponding indices etc.)
Was This Post Helpful? 0
  • +
  • -

#15 underoath23  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 22
  • Joined: 05-December 10

Re: Array/Random Help

Posted 07 February 2011 - 07:06 PM

I am having trouble generating the numbers before the loops. I have tried moving stuff around but I am used to c++ and am really confused. Please Help

package project2Pak;

import java.util.*;

public class ProjectTwo
{
	
	public static void main(String[] args)
	{
		
		Random randNumGenerator = new Random();
		System.out.print("The Numbers Are ");
 
		int[] nums = new int[11];
		
		
		for (int i=1; i<nums.length; i++)
		{
			nums[i] = (randNumGenerator.nextInt(10)+1);
			System.out.print(nums [i] + " ");
		}
		for (int i = nums.length -1; i >=1 ; i--)
		{
			nums[i] = (randNumGenerator.nextInt(10)+1);
			System.out.println(nums[i]);
		}
		
	}
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2