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=newVal) { index = i; found=true; break; } } if(found) { for (int i = count; i > index; i--) { arr[i] = arr[i-1]; } }else { index=count; } arr[index] = newVal; return true; }

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

Command Prompt - D * $ java Project4 P4input.txt 15 19 32 34 38 42 46 50 57 60 65 67 71 79 85 89 90 92 98 99 100 Command Prompt - D * $ java Project4 P4input.txt 15 19 32 34 38 42 46 50 57 60 65 67 71 79 85 89 90 92 98 99 100

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!