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

Please make sure you conform to the following requirements: 1. Create a global c

ID: 3607228 • Letter: P

Question

Please make sure you conform to the following requirements: 1. Create a global constant integer called CAPACITY and set it to 100 (5 points) Use the class examples and exercises to write functions to initialize the array by reading values from the user, and to print the array. These functions take the array and its current size as parameters. You can use the code in the examples. (5 points) 3. Write a function called insert that takes the array, a number, a position and the current size 4. Write a function called remove that takes the array, a number and the current size of the 5. Write a function called sort that takes the array and its current size as parameters and sorts 6. In the main function, have the user initialize the array. Then, write a command line menu of the array as parameters. Insert the number at the given position. (15 points) array, and removes the first instance of the number from the array. (15 points) the array in ascending order. You need to use insertion sort, which is given below. (15 points) with the following options: e Print » Insert » Remove » Sort » Exit Call the appropriate functions when the user chooses a particular option. Print and error message and continue if the user enters a wrong option. (15 points) 7. Please make sure your code is appropriately commented. (5 points) Algorithm for insertion sort

Explanation / Answer

//Writing the coed in JAVA as no programming language has been mentioned

//source code

package chegg;

import java.util.Scanner;

public class Array_use {

public static final int CAPACITY = 100; //global varibale

public static int size=0; //stores the size of the array

public static void printArray(int A[]) // print the array

{

int i=0;

for(i=0;i<size;i++)

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

System.out.println("");

}

public static void readArray(int A[]) {

int i=0;

System.out.println("How Many element you want to insert");

Scanner sc = new Scanner(System.in);

int len = sc.nextInt();

size = len;

System.out.println("Enter the element separated by space");

for(i=0;i<size;i++)

A[i]=sc.nextInt();

return;

}

public static void insert(int A[], int n, int index) { //inserts n into A[] at A[index] the right shifting all

int temp1,temp2=A[index];

if(size==CAPACITY)

{

System.out.println("Capacity Full"); // return if capacity full

return;

}

for(int i=index;i<A.length-1;i++)

{

temp1=A[i+1];

A[i+1]=temp2;

temp2=temp1;

}

A[index]=n;

size++;

}

/* returns the index of the first occurrence of

* x in A[] or -1 if x doesn't exist in A[] */

public static int find(int A[], int x) {

int i=0;

int index=-1;

for(i=0;i<A.length;i++)  

{

if(A[i]==x)

return i; //exiting from function by returning index of the element

}

return index;

}

public static void remove(int A[], int n) { //inserts n into A[] at A[index] the right shifting all

if(size==0)

{

System.out.println("Nothing to remove"); // if the array is empty

return;

}

int index = find(A,n);

if(index==-1) //element is not there in the array

{

System.out.println("Element does not exist in the array");

return;

}

for(int i=index;i<size-1;i++)

{

A[i]=A[i+1];

}

size--;

}

/*Function to sort array using insertion sort*/

public static void sort(int arr[])

{

int n = size;

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

{

int key = arr[i];

int j = i-1;

/* Move elements of arr[0..i-1], that are

greater than key, to one position ahead

of their current position */

while (j>=0 && arr[j] > key)

{

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

j = j-1;

}

arr[j+1] = key;

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

int A[] = new int[CAPACITY];

readArray(A);

printArray(A);

Scanner sc = new Scanner(System.in);

while(true)

{

System.out.println("Enter your choice");

System.out.println("1.Print");

System.out.println("2.Insert");

System.out.println("3.Remove");

System.out.println("4.Sort");

System.out.println("5.Exit");

int input = sc.nextInt();

if(input==1)

printArray(A);

else if(input==2)

{

System.out.println("Enter the number to insert");

int n=sc.nextInt();

System.out.println("Enter the index to insert");

int index=sc.nextInt();

insert(A, n, index);

}

else if(input==3)

{

System.out.println("Enter the number to remove");

int n=sc.nextInt();

remove(A, n);

}

else if(input==4)

{

sort(A);

}

else if(input==5)

{

System.out.println("Good Bye");

break;

}

else

System.out.println("Wrong input");

}

}

}

//sample output//

How Many element you want to insert
10
Enter the element separated by space
10 40 30 50 60 20 60 70 90 80
10 40 30 50 60 20 60 70 90 80  
Enter your choice
1.Print
2.Insert
3.Remove
4.Sort
5.Exit
1
10 40 30 50 60 20 60 70 90 80  
Enter your choice
1.Print
2.Insert
3.Remove
4.Sort
5.Exit
2
Enter the number to insert
25
Enter the index to insert
4
Enter your choice
1.Print
2.Insert
3.Remove
4.Sort
5.Exit
1
10 40 30 50 25 60 20 60 70 90 80  
Enter your choice
1.Print
2.Insert
3.Remove
4.Sort
5.Exit
3
Enter the number to remove
20
Enter your choice
1.Print
2.Insert
3.Remove
4.Sort
5.Exit
1
10 40 30 50 25 60 60 70 90 80  
Enter your choice
1.Print
2.Insert
3.Remove
4.Sort
5.Exit
4
Enter your choice
1.Print
2.Insert
3.Remove
4.Sort
5.Exit
1
10 25 30 40 50 60 60 70 80 90  
Enter your choice
1.Print
2.Insert
3.Remove
4.Sort
5.Exit
7
Wrong input
Enter your choice
1.Print
2.Insert
3.Remove
4.Sort
5.Exit
5
Good Bye