Program with 200 random numbers?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 739 Views - Last Post: 09 May 2013 - 01:15 PM Rate Topic: -----

#1 hansma  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 04-May 13

Program with 200 random numbers?

Posted 04 May 2013 - 08:30 PM

I am confused on how to do this, so i was wondering if anyone could help me create a code that will:
· generate 200 random numbers from 1 to 1000 and
o store them in an array.
· Sort the array using a sorting algorithm
· Generate one more random number in the range of 1 to 1000, name it target
· Use the Binary Search algorithm to check if the target value is in your array.
o IF target IS in the array, output the position it was found in.
· For the DISPLAY:
o Display the array, 20 numbers per line
o The target value,
o If not found….simply state target was not in the list
o IF found, state that target was found in position 7. (which would be index value 6)

thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Program with 200 random numbers?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9098
  • View blog
  • Posts: 34,183
  • Joined: 12-June 08

Re: Program with 200 random numbers?

Posted 04 May 2013 - 08:41 PM

What have you tried? What parts do you think you need?
Was This Post Helpful? 0
  • +
  • -

#3 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Program with 200 random numbers?

Posted 04 May 2013 - 08:42 PM

You might want to start here:
- Oracle Docs > Random
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Program with 200 random numbers?

Posted 05 May 2013 - 01:26 AM

I would guess that you need to do it by hand so this might not be a huge help but be aware that java.util.Arrays is a nice utility class.

Tweak it, complete it, play with it.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 hansma  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 04-May 13

Re: Program with 200 random numbers?

Posted 06 May 2013 - 04:14 PM

well this is what ive got so far but all i get is errors that i dont know how to fix.

public class RandomNumbers{
	public static int binarySearch(String[] sorted, String key){
		int[] rNumb = new int[200];
		Random r = new Random();
		for(int i = 0; i < rNumb.length; i++)
		rNum[i] = r.nextint(999) + 1;

		int randomRandom = r.nextint(999) + 1;
		}
}


Was This Post Helpful? -1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Program with 200 random numbers?

Posted 06 May 2013 - 04:26 PM

So ???
WHEN YOU HAVE AN ERROR POST IT !!!
Why should we redo the compiler's job ?
Was This Post Helpful? 0
  • +
  • -

#7 hansma  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 04-May 13

Re: Program with 200 random numbers?

Posted 06 May 2013 - 04:33 PM

It says it cant find symbol and then points at the Capital Randoms in line 4 and the first r in line 6
Was This Post Helpful? -1
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10492
  • View blog
  • Posts: 38,876
  • Joined: 27-December 08

Re: Program with 200 random numbers?

Posted 06 May 2013 - 05:03 PM

Did you import Random from the java.util package?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Program with 200 random numbers?

Posted 06 May 2013 - 07:12 PM

You probably missed the import java.util.Random; or java.util.*;

I have to guess based on YOUR output

Posting the error is not displaying your interpretation of the error is to post the actual output of the Java compiler
Was This Post Helpful? 0
  • +
  • -

#10 hansma  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 04-May 13

Re: Program with 200 random numbers?

Posted 09 May 2013 - 11:16 AM

Ok, so this is my code but when i compile and run it all i get is a bunch of zeros and -201, but i want random numbers and for it to tell me if the random number, in this case -201 was found. can someone help me?

import java.util.Arrays;

class Test

{

    public static void main(String[] args){

 
        int[] array = createRandomArray(200);
 
        Arrays.sort(array);

        int searchValue = 24;

        int value = Arrays.binarySearch(array, searchValue);

        System.out.println(Arrays.toString(array));

        System.out.println(value);
    }
    private static int[] createRandomArray(int 200){

        int[] arr = new int[200];

        for(int i=0; i < size; i++){

        }

        return arr;

    }

}


Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Program with 200 random numbers?

Posted 09 May 2013 - 11:22 AM

    for(int i=0; i < size; i++){

    }

There is something missing.. this loop does nothing.

And where does size come from?
Was This Post Helpful? 0
  • +
  • -

#12 hansma  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 04-May 13

Re: Program with 200 random numbers?

Posted 09 May 2013 - 11:25 AM

View Postandrewsw, on 09 May 2013 - 11:22 AM, said:

    for(int i=0; i < size; i++){

    }

There is something missing.. this loop does nothing.

And where does size come from?


so i should completely delete that loop?
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Program with 200 random numbers?

Posted 09 May 2013 - 11:29 AM

View Posthansma, on 09 May 2013 - 06:25 PM, said:

so i should completely delete that loop?

Er, no. You need to fill the array with random numbers:

Quote

generate 200 random numbers from 1 to 1000

Was This Post Helpful? 0
  • +
  • -

#14 hansma  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 10
  • Joined: 04-May 13

Re: Program with 200 random numbers?

Posted 09 May 2013 - 11:35 AM

How about this is this any better?
class Test

{

    public static void main(String[] args){

 
        int[] array = createRandomArray(200);
 
        Arrays.sort(array);

        int searchValue = 24;

        int value = Arrays.binarySearch(array, searchValue);

        System.out.println(Arrays.toString(array));

        System.out.println(value);
    }
    private static int[] createRandomArray(int value200){

        int[] arr = new int[200];
	
	for(int i=0; i < 1; i++){

    }
	return;
	}
}


Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Program with 200 random numbers?

Posted 09 May 2013 - 11:45 AM

View Posthansma, on 09 May 2013 - 06:35 PM, said:

How about this is this any better?

No, it's worse. I suggest that you visit the Tutorials section here @DIC and study the fundamentals of Java.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2