In: Computer Science
The purpose of this lab is to manipulate an array of integers. The assignment is to write a program that: 1. Declares an array of integers of size equal to constant int SIZE = 10. 2. Implements the following methods: A method that displays a menu. The menu should be displayed after each completed menu selection. A method that prompts the user to initialize the array. The entire array does not need to be initialized. The user may enter only as many (or as few) values as they would like in the array. The user will enter a negative value to indicate no more to be added. Hint: This implies that the number of elements in the array does not necessarily have to be the same as the maximum size of the array. A method that displays the contents of the array. Only elements currently contained in the array should be horizontally displayed. A method that displays the minimum, maximum, sum and average of all the elements currently contained in the array. A method that accepts a number and determines how many times (if any) that number appears as an element in the array. A method that allows the user to add a number to the end of the array. A method that allows you to insert a specified number at a specified index within the currently filled portion of the array. Hint: In order to accomplish this, the method must first check to see if there is room in the array to accept a new number. If the array is currently full (e.g. number of elements equals the maximum size of the array) then the user has to first delete an element from the array. Think carefully about what an insert into the array implies. The physical size of the array cannot change, therefore what must happen if a value is to be inserted somewhere in the middle of the array without losing any of the existing elements. A method that removes an element at a specified index in the array. Hint: As with the insert method, the physical size of the array cannot change, therefore how could we simulate an element of the array being deleted? CSCI 125 Array Manipulation Prof. Kadri Page 2 of 4 Sample run: Menu only displayed once here. Your program should display the menu after each completed menu selection. 1. Initialize Array 2. Display Array 3. Add element to the end 4. Add an element at a specific index 5. Remove an element at specific index 6. Show min, max, sum and average 7. Search 8. Exit 1. Initialize Array Enter integer values to fill the array -ve value to stop: 1 2 3 4 5 6 8 -9 2. Display Array 1 2 3 4 5 6 8 6. Show min, max, sum and average Min=1 Max=8 Sum=29 Average=4.14 7. Search Enter number to search for: 2 2 occurs 1 time. 5. Remove an element at specific index Enter index between 0 and 6 7 Invalid index Enter index between 0 and 6 1 2. Display Array 1 3 4 5 6 8 4. Add an element at a specific index Enter index between 0 and 5 6 Invalid index Enter index between 0 and 5 3 Enter number to insert 9 CSCI 125 Array Manipulation Prof. Kadri Page 3 of 4 2. Display Array 1 3 4 9 5 6 8 1. Initialize Array Enter integer values to fill the array -ve value to stop: 1 2 3 4 5 6 7 8 9 10 11 -9 Array is full. One or more numbers could not be inserted 2. Display Array 1 2 3 4 5 6 7 8 9 10 3. Add element to the end Array is full. 8. Exit Goodby import java.util.Scanner; public class ArrayMenu { static int count; static Scanner kb = new Scanner(System.in); public static void main() { int item=0; int[] numArray=new int[100]; count=0; while (item !=8) { menu(); item=kb.nextInt(); if (item==1) initializeArray(numArray); else if (item==2) printArray(numArray); } System.out.println("Goodby!"); } public static void menu() { System.out.println("1. Initialize Array"); System.out.println("2. Display Array"); System.out.println("3. Add element to the end"); System.out.println("4. Add an element at a specific index"); System.out.println("5. Remove an element at specific index"); System.out.println("6. Show min, max, sum and average"); System.out.println("7. Search"); System.out.println("8. Exit"); System.out.print(": "); } public static void initializeArray(int[] arr) { count=0; int num; System.out.print("Enter integer values to fill the array -vevalue to stop: "); do { num = kb.nextInt(); if (num >=0) { arr[count]=num; count++; } } while (num > 0); } public static void printArray(int[] arr) { for (int i=0; i< count; i++) System.out.print(arr[i]+" "); System.out.println(); } }
CODING LANGAUGE JAVA
import java.util.Scanner;
import java.util.Scanner;
class ArrayMenu {
static int count;
static Scanner kb = new Scanner(System.in);
public static void main(String[] args) {
int item = 0;
int[] numArray = new int[100];
count = 0;
while (item != 8) {
menu();
item = kb.nextInt();
if (item == 1)
initializeArray(numArray);
else if (item == 2)
printArray(numArray);
else if (item == 3)
add(numArray);
else if (item == 4)
add_index(numArray);
else if (item == 5)
remove(numArray);
else if (item == 6)
show(numArray);
else if (item == 7)
Search(numArray);
else
break;
}
System.out.println("Goodby!");
}
public static void menu() {
System.out.println("1. Initialize Array");
System.out.println("2. Display Array");
System.out.println("3. Add element to the end");
System.out.println("4. Add an element at a specific index");
System.out.println("5. Remove an element at specific index");
System.out.println("6. Show min, max, sum and average");
System.out.println("7. Search");
System.out.println("8. Exit");
System.out.print(": ");
}
public static void initializeArray(int[] arr) {
count = 0;
int num;
System.out.print("Enter integer values to fill the array -vevalue to stop: ");
do {
num = kb.nextInt();
if (num >= 0) {
arr[count] = num;
count++;
}
} while (num > 0);
}
public static void printArray(int[] arr) {
for (int i = 0; i < count; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void add(int[] arr) {
// read element from user and insert to arr
System.out.print("Enter element to insert at end: ");
arr[count] = kb.nextInt();
count++;
}
public static void add_index(int[] arr) {
// read index and element from user and insert to arr
System.out.print("Enter index at which value is to be inserted: ");
int index = kb.nextInt();
System.out.print("Enter element to insert: ");
// shift all elements after index to right
for (int i = count; i > index; i--) {
arr[i] = arr[i - 1];
}
arr[index] = kb.nextInt();
count++;
}
// System.out.println("5. Remove an element at specific index");
public static void remove(int[] arr) {
// read index from user
System.out.print("Enter index from which value is to be removed: ");
int index = kb.nextInt();
// shift all elements after index to left
for (int i = index + 1; i < count; i++) {
arr[i - 1] = arr[i];
}
count--;
}
// System.out.println("6. Show min, max, sum and average");
public static void show(int[] arr) {
// declare required variables
int min = arr[0], max = arr[0], sum = 0;
// calculate
for (int i = 0; i < count; i++) {
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
sum += arr[i];
}
// calculate average
float average = (float) sum / count;
// display results
System.out.println("\nMinimum element: " + min);
System.out.println("Maximum element: " + max);
System.out.println("Sum: " + sum);
System.out.println("Average: " + average + "\n");
}
public static void Search(int[] arr) {
// read element to search
System.out.print("Enter element to search: ");
int search = kb.nextInt();
int found = -1;
// search in array
for (int i = 0; i < count; i++) {
if (arr[i] == search) {
found = i;
break;
}
}
if (found != -1)
System.out.println(search + " found at index " + found);
else
System.out.println(search + " not found");
}
}
Output: