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

Exercise 18: Deleting Array Elements Folder name A170 E18 Your LastName YourFirs

ID: 3832488 • Letter: E

Question

Exercise 18: Deleting Array Elements Folder name A170 E18 Your LastName YourFirstName Write a class DeleteElements that prints out an array and asks the user which number should be deleted and then deletes that number, if it exists from the array. Step 1 o Start by simply searching the element. o Pseudocode: Create an array and populate it with random positive integers. Print the array to showthe user which elements the array contains Use a WHILEloop to search the element- a WHILE loop is better than a FOR loop because you can stop when the element is found by using a Boolean value in the loop condition--and also keep track of the location where the element might be found. If the element is not found, output the error message, "Number not found. Otherwise output the message, "Number found at index r. where is the index where the element is. Print out the array. o Test your program by searching The first element in the array The last element in the array Any middle element in the array A number that is not in the array Example output 34 65 12 76 45 39 86 71 67 Number to delete: 76 r found at index 3

Explanation / Answer

Please find my implementation.

Please let me know in case of any issue.

import java.util.Random;

import java.util.Scanner;

public class DeleteElements {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       System.out.print("How many number in array ? ");

       int n = sc.nextInt();

       //creating array and filling with random data

       int[] arr = new int[n];

       Random random = new Random();

       for(int i=0; i<n; i++)

           arr[i] = random.nextInt(100)+1; // in range 1-100

       for(int i=0; i<arr.length; i++)

           System.out.print(arr[i]+" ");

       System.out.println();

       int current_length = arr.length;

       int key, i;

       char op = 'y';

       do{

           System.out.print("Number to delete: ");

           key = sc.nextInt();

           i=0;

           while(i < current_length){

               if(arr[i] == key)

                   break;

               i++;

           }

           if(i == current_length){

               System.out.println("Number not found");

           }else{

               System.out.println("Number found at index "+i);

               for(int j=i; j<current_length-1; j++)

                   arr[j] = arr[j+1];

               current_length = current_length - 1;

           }

           if(current_length == 0){

               System.out.println("The array is now empty");

               break;

           }else{

               for(int j=0; j<current_length; j++)

                   System.out.print(arr[j]+" ");

               System.out.println();

           }

           System.out.print("Try again (y/n)? ");

           op = sc.next().charAt(0);

       }while(op=='y' || op=='Y');

   }

}

/*

Sample run:

How many number in array ? 5

60 60 50 40 9

Number to delete: 60

Number found at index 0

60 50 40 9

Try again (y/n)? y

Number to delete: 8

Number not found

60 50 40 9

Try again (y/n)? y

Number to delete: 40

Number found at index 2

60 50 9

Try again (y/n)? y

Number to delete: 9

Number found at index 2

60 50

Try again (y/n)? y

Number to delete: 60

Number found at index 0

50

Try again (y/n)? y

Number to delete: 50

Number found at index 0

The array is now empty

*/