Question: I need help finishing this code, and the directions are posted as comments within the code. You are not allowed to leave in the call

I need help finishing this code, and the directions are posted as comments within the code. You are not allowed to leave in the call to Arrays.sort() that we put in your starter file. That call is just to ensure that your insertInOrder is getting the correct index value to work with so you can debug your insertInOrder before you work on your binary search.

import java.util.*; import java.io.*;

public class Project3 { static final int INITIAL_CAPACITY = 5;

public static void main( String args[] ) throws Exception { if (args.length

int numInts2generate = Integer.parseInt( args[0] );

// WE USE Random number generator to fill our array

Random randGen = new Random( 17 ); // SEED with 17 int[] arr = new int[INITIAL_CAPACITY]; int count= 0; for ( int i = 0 ; i { if ( count==arr.length ) arr = upSizeArr(arr); insertInOrder( arr, count++, 1 + randGen.nextInt(1000) ); }

arr=trimArr(arr,count); // Now count == .length printArray( arr ); // we trimmed it thus count == length so we don't bother to pass in count

} // END MAIN

// ############################################################################################################

static void printArray( int[] arr ) { for( int i=0 ; i System.out.print(arr[i] + " " ); System.out.println(); }

static int[] upSizeArr( int[] fullArr ) { int[] upSizedArr = new int[ fullArr.length * 2 ]; for ( int i=0; i upSizedArr[i] = fullArr[i]; return upSizedArr; }

static int[] trimArr( int[] oldArr, int count ) { int[] trimmedArr = new int[ count ]; for ( int i=0; i trimmedArr[i] = oldArr[i]; return trimmedArr; }

// REMOVE ALL COMMENTS FROM INSERT IN ORDER JUST BEFORE HANDIN static void insertInOrder( int[] arr, int count, int newVal ) { // WAIT TILL YOUR PROGRAM PRODUCES CORRECT OUTPUT. // THEN REPLACE THE LINE BELOW WITH A CALL TO -YOUR- BSEARCH int index = Arrays.binarySearch( arr, 0, count, newVal ); //if index is negative, convert/decode back to non negative

// write a loop that opens up a slot at [index] arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE }

// REMOVE ALL COMMENTS FROM BSEARCH JUST BEFORE HANDIN static int bSearch(int[] a, int count, int key) { return 0; // JUST TO MAKE IT COMPILE. /* DEFINE & INITIALIZE LO, MID AND HI

while ( lo

return encoded form of low which is add one then negate */ } } // END PROJECT3

The ouput should look like this: I need help finishing this code, and the directions are posted

Command Prompt ons> java Project3 58 63 96 108 154 221 231 236 264 269 320 405 449 459 464 517 528 538 651 694 695 721 722 771 809 893 916 948 975 977 D:NDesktop project-03 solutions>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!