Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/** * (name header) */ import java.util.Scanner; public class InsertInOrder { pu

ID: 3766708 • Letter: #

Question

/**
* (name header)
*/

import java.util.Scanner;

public class InsertInOrder
{
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
  
int [] a = new int [5];   
int numOfElements = 0;
  
boolean tryAgain = true;
  
System.out.print("Number to insert: ");
int numToInsert = input.nextInt();
  
if (numToInsert != 0)
{
a[0] = numToInsert; // insert first number
++numOfElements; // increment number of elements
// print first number
System.out.print("Array is now: ");
System.out.println(a[0]);   
}
  
while (numOfElements < a.length)
{   
System.out.print("Number to insert: ");
numToInsert = input.nextInt();
  
//Your code here...
  
  
  
// print array
System.out.print("Array is now: ");
for (int i = 0; i < numOfElements; ++i)
{
System.out.print(a[i] + " ");
}
System.out.println();
}
  
System.out.println(" Array is full.");
}
}

Complete the project InsertlnOrder so that the user can enter positive integers to be stored in ascending order in an array of capacity 5. The project already contains code to declare an array and insert the first number. Once the first number is inserted a while loop start to iterate until the array is full. The loop will insert the elements in the array and print the array after each insertion. Once the array is full, a message will be displayed to end the program. Your job is to complete the while loop. Ask the user which number to insert and store the number in a variable. IF the number to insert is larger than the last number in the array Insert the number at the end of the array and increment the number of elements. ELSE //start traversing the array to find where to insert the number WHILE the location is not found and the end of the array has not been reached IF the number to insert is smaller or equal the element at this index Use a FOR loop to shift elements Insert the element Increment number of elements ELSE Move forward How do you insert an element in the array? o You shift elements to the left to "make space" for the new number. Example: If you want to insert 34, you should insert it at index 2. Starting from the end of the list, shift each element to its right. A number smaller than the first element A number larger than the last element A number in between the first and last element A number already in the array

Explanation / Answer

import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class InsertInOrder {
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);

       int[] a = new int[5];
       int numOfElements = 0;

       System.out.print("Number to insert: ");
       int numToInsert = input.nextInt();

       if (numToInsert != 0) {
           a[0] = numToInsert; // insert first number
           ++numOfElements; // increment number of elements
                               // print first number
           System.out.print("Array is now: ");
           System.out.print(a[0]);
       }

       while (numOfElements < a.length) {
           System.out.print(" Number to insert: ");
           numToInsert = input.nextInt();

           // Your code here...

           int j;
           for (j = 0; j < numOfElements; j++)
               // find where it goes
               if (a[j] > numToInsert) // (linear search)
                   break;
           for (int k = numOfElements; k > j; k--)
               // move bigger ones up
               a[k] = a[k - 1];
           a[j] = numToInsert; // insert it
           numOfElements++; // increment size

           System.out.print("Array is now: ");
           for (int i = 0; i < numOfElements; ++i) {
               System.out.print(a[i] + " ");
           }
       } // end insert()

       // print array

   }

}

OUTPUT:

Number to insert: 5
Array is now: 5
Number to insert: 2
Array is now: 2 5
Number to insert: 7
Array is now: 2 5 7
Number to insert: 4
Array is now: 2 4 5 7
Number to insert: 5
Array is now: 2 4 5 5 7