In: Computer Science
we are looking at the "changeSpecificIndex" method.
i was able to replace the value of that specific index, but im stumped at adding a value to a specific index AND shifting the values after it to the next index.
Here is my code
_____________________________________________
import java.util.Scanner;
import java.util.Arrays;
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);
else if (item==3)
addToEnd(numArray);
else if (item == 4)
changeSpecificIndex(numArray);
/* else if (item == 5)
removeElement(numArray);
*/ else if (item == 6)
minMaxSumAvg(numArray);
/* else if (item == 7)
Search(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.println("________________________________________");
}
public static void initializeArray(int[] arr){
count=0;
int num;
System.out.print("Enter integer values to fill the array -value 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 addToEnd(int[] arr){
System.out.print("Enter number to add: ");
int num = kb.nextInt();
arr[count] = num;
count++;
System.out.println();
}
public static void minMaxSumAvg(int[] arr){
double max = arr[0];
double min = arr[0];
double sum = 0;
double avg = 0;
for(int i = 1; i < count; i++){
int numby = arr[i];
if(numby > max){
max = numby;
}
if(numby < min){
min = numby;
}
sum = sum + arr[i];
}
avg = sum / count;
System.out.println("Sum is: " + sum);
System.out.println("Average is: " + avg);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
public static void changeSpecificIndex(int[] arr){
int arrIndex;
int newValue;
System.out.println("What index would you like to change: ");
arrIndex = kb.nextInt() - 1;
System.out.println("Assign new value: ");
newValue = kb.nextInt();
arr[arrIndex] = newValue;
System.out.println("The value of Index " + (arrIndex + 1) + " has
been changed to " + newValue);
}
}
import java.util.Scanner; import java.util.Arrays; public 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) addToEnd(numArray); else if (item == 4) changeSpecificIndex(numArray); /* * else if (item == 5) removeElement(numArray); */ else if (item == 6) minMaxSumAvg(numArray); /* * else if (item == 7) Search(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.println("________________________________________"); } public static void initializeArray(int[] arr) { count = 0; int num; System.out.print("Enter integer values to fill the array -value 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 addToEnd(int[] arr) { System.out.print("Enter number to add: "); int num = kb.nextInt(); arr[count] = num; count++; System.out.println(); } public static void minMaxSumAvg(int[] arr) { double max = arr[0]; double min = arr[0]; double sum = 0; double avg = 0; for (int i = 1; i < count; i++) { int numby = arr[i]; if (numby > max) { max = numby; } if (numby < min) { min = numby; } sum = sum + arr[i]; } avg = sum / count; System.out.println("Sum is: " + sum); System.out.println("Average is: " + avg); System.out.println("Max is: " + max); System.out.println("Min is: " + min); } public static void changeSpecificIndex(int[] arr) { int arrIndex; int newValue; System.out.println("What index would you like to change: "); arrIndex = kb.nextInt() - 1; System.out.println("Assign new value: "); newValue = kb.nextInt(); if(arrIndex > count) { System.out.println("Invalid Index entered. Valid indices are from 0 to " + count); } else { for(int i=count; i>arrIndex; i--) { arr[i] = arr[i-1]; } arr[arrIndex] = newValue; System.out.println("The value of Index " + (arrIndex ) + " has been changed to " + newValue); count++; } } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.