Array Of Varying Size
Hi
I need to create an array of user defined length. Basically i want to allow the user to enter the length in an dialog box or similar and then the array will be created of that size.
The array will then be filled with randomly generated numbers.
Then i need the array to be sorted quickly.
I CAN THEN APPLY THE BINARY SEARCH!
Can anyone help with the coding for the above task please. My actual binary search coding is fine its just the creating the array and so on that im having difficulties with.
Thnx in advance
[560 byte] By [
Princea] at [2007-10-2 5:33:16]

int n = Integer.parseInt(...); // input from user
if (n < 0)
throw new RuntimeException("value >= 0 required");
int[] arr = new int[n];
for (int i = 0; i < n.length; i++) {
arr[i] = ... Math.random() * ... ;
java.util.Arrays.sort(arr);
// do binary search
public class Binary_Search
{
int n = Integer.parseInt(...); // input from user
if (n < 0)
throw new RuntimeException("value >= 0 required");
int[] arr = new int[n];
for (int i = 0; i < n.length; i++) {
arr = ... Math.random() * ... ;
java.util.Arrays.sort(arr);
public int binarySearch(int arr[],int key)
{
int low=0;
int high=arr.length-1;
while(low<=high)
{
int middle=low + (high - low)/2;
if(key==arr[middle])
return middle;
else
if(key<arr[middle]) high=middle-1;
else low=middle+1;
}
return -1;//not found
}}
Would that be the full code mate?>