`
FACULTY OF BUSINESS AND INFORMATION SCIENCE
ASSIGNMENT COVER PAGE
1 7 1 1 2 4
Student Id :
Student Name : Priyanka Maharjan
Email : priy171124@[Link]
Course Code : CC105N
Course Name : Data Structure & Algorithm
Semester : 7th Semester
Session : Jan-April, 2019
Instructor/Examiner/Lecturer : Mr. Mohan Maharjan
Assignment No. :1
Assignment Type : Individual
Submission Date :
Marks Obtained : …………… out of …………….
Assignment 1
Assignment 1
Write a Java program which a user can enter an index of an integer array in order to insert
or delete an item. The following program output can give you a better picture of how it
looks like.
Code:
// package indexinteger;
import [Link];
import [Link];
/**
*""
* @author user
*/
public class indexInteger {
// Function to remove the element
public static int[] removeTheElement(int[] b, int indexvalue) {
// If the array is empty
// or the index is not in array range
// return the original array
if (b == null || indexvalue < 0 || indexvalue >= [Link]) {
return b;
}
// Create another array of size one less
int[] anotherArray = new int[[Link] - 1];
// Copy the elements except the index
// from original array to the other array
for (int i = 0, k = 0; i < [Link]; i++) {
// if the index is
// the removal element index
if (i == indexvalue) {
continue;
}
// if the index is not
// the removal element index
anotherArray[k++] = b[i];
}
// return the resultant array
return anotherArray;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner inp = new Scanner([Link]);
int a[] = new int[7];
a[0] = 11;
a[1] = 22;
a[2] = 33;
a[3] = 44;
a[4] = 55;
a[5] = 66;
a[6] = 77;
int b[] = new int[8];
boolean check = true;
boolean checkTwo = true;
[Link]("Before instering an number");
for (int i = 0; i < [Link]; i++) {
[Link](a[i]);
}
[Link]("Enter an number to insert");
int number = [Link]();
while (check) {
try {
[Link]("Enter an index value to be insert");
int index = [Link]();
if (index < [Link]) {
check = false;
} else {
[Link]("Enter again");
}
if (check == false) {
for (int i = 0; i < [Link]; i++) {
if (i < index) {
b[i] = a[i];
} else if (i == index) {
b[i] = number;
} else {
b[i] = a[i - 1];
}
}
} catch (Exception e) {
[Link](e);
}
}
[Link]("After Instering a value");
for (int i = 0; i < [Link]; i++) {
[Link](b[i]);
}
// Print the resultant array
[Link]("Before delete array Array: " + [Link](b));
while (checkTwo) {
try {
// Get the specific index
[Link]("Enter an index to delete");
int indexvalue = [Link]();
if (indexvalue < [Link]) {
checkTwo = false;
} else {
[Link]("Enter again");
}
if (checkTwo == false) {
// Print the index
[Link]("Index to be removed: " + indexvalue);
// Remove the element
b = removeTheElement(b, indexvalue);
// Print the resultant array
[Link]("Resultant Array: " + [Link](b));
}
}catch(Exception e){
[Link](e);
}
}
}
}
Output: