Question: Hello. This program produces the correct output but there is a couple of modifications that are needed that I cant figure out. Please fix these:
Hello. This program produces the correct output but there is a couple of modifications that are needed that I cant figure out. Please fix these:
1. INSERT_IN_ORDER USED MORE THAN ONE LOOP
2. INSERT_IN_ORDER COMPARED INCOMING NEWVAL AGAINST ARRAY ELEMENTS. BSEARCH ALREADY DID THAT
import java.util.*; import java.io.*;
public class Project4 { static final int INITIAL_CAPACITY = 5;
public static void main( String args[] ) throws Exception { if (args.length
Scanner infile = new Scanner( new File( args[0] ) ); int[] arr = new int[INITIAL_CAPACITY]; int count= 0;
while ( infile.hasNextInt() ) { if ( count==arr.length ) arr = upSizeArr(arr); if (insertInOrder( arr, count, infile.nextInt() ) ) ++count; }
arr=trimArr(arr,count); // Now count == .length printArray( arr ); // we trimmed it thus count == length so we don't bother to pass in count
}
// ############################################################################################################
static void printArray( int[] arr ) { for( int i=0 ; i static int[] upSizeArr( int[] fullArr ) { int[] upSizedArr = new int[ fullArr.length * 2 ]; for ( int i=0; i static int[] trimArr( int[] oldArr, int count ) { int[] trimmedArr = new int[ count ]; for ( int i=0; i static boolean insertInOrder( int[] arr, int count, int newVal ) { int index = bSearch( arr, count, newVal ); if ( index >= 0 ) return false; boolean found=false; for(int i=0;i static int bSearch(int[] a, int count, int key) { int l = 0, r = count - 1; while (l if (a[m] == key) { return m; } if (a[m] else { r = m - 1; } } return -1; } }// END PROJECT4 EXPECTED OUTPUT
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
