Complete the project InsertInOrder so that the user can enter positive integers
ID: 3835109 • Letter: C
Question
Complete the project InsertInOrder so that the user can enter positive integers to be stored in ascending order in an array of capacity S. 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 writ 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. Pseudocode: 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 EISC//start traversing the array to find where to inert 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 dements Insert the element Increment number of elements ELSE Move forward How do you insert an element in the array? 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 Overwrite element at index 2 with element to insert. Test your program by inserting: A number smaller than the first element A number larger than the last dement A number in between the first and last element A number already in the arrayExplanation / Answer
public class Insert {
// this method used to print elements from array. starting from
// 0th index to lastNoIndex
public static void print(int arr[], int lastNoIndex) {
for (int i = 0; i <= lastNoIndex; i++) {
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
int[] arr = new int[5];
Scanner sc = new Scanner(System.in);
int lastNumIndex = -1; // to check last num in array.
// while loop keep iterating until the array is full
while (lastNumIndex < arr.length - 1) {
// scanner class used to get input from console
System.out.print(" Enter no:");
// nextInt() method returns the next int from console if input is available.
int no = sc.nextInt();
// if array is empty i.e we use lastNumIndex to check if there are any elements
// we have inserted.
if (lastNumIndex == -1) {
// insert element at 0 position and increment pointer
arr[lastNumIndex + 1] = no;
lastNumIndex++;
// this method print the elements from the array.
print(arr, lastNumIndex);
} else if (no > arr[lastNumIndex]) {
arr[lastNumIndex + 1] = no;
lastNumIndex++;
print(arr, lastNumIndex);
} else {
// start from the first index and check if "no" is less than
// the index. if not, increment index.
int index = 0;
while (lastNumIndex != arr.length - 1) {
if (no <= arr[index]) {
// this is used to swap element by one index from right to left
for (int i = lastNumIndex; i >= index; i--) {
arr[i + 1] = arr[i];
}
arr[index] = no;
lastNumIndex++;
print(arr, lastNumIndex);
// break the loop and ask for another number.
break;
} else {
/// move foreward
index++;
}
}
}
}
if (lastNumIndex == arr.length - 1) {
System.out.println(" Array is full.");
System.exit(0);
}
}
}
Hope this helps. comment here if in case of doubt.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.