Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; import java.util.Arrays; public class Lab 7 Template { public static void main ( String [ ] args ) { / / = =

import java.util.Scanner;
import java.util.Arrays;
public class Lab7Template
{
public static void main(String[] args)
{
//============================================================
// Declare some variables. You might need
//
//- an integer for the array size,
//- an integer for the exchange element, and
//- a scanner object for requesting input from System.in
//
// Scanner object for user input
Scanner scanner = new Scanner(System.in);
int size;
int exchangeElement;
//============================================================
// Request array size from the user
//
System.out.print("How many elements in the array: ");
int size = scanner.nextInt();
// Declare an int array by the size you received above
int[] array = new int[size];
// Request an integer from the user and save it
//============================================================
// Fill in the Array
for (int i =0; i < size; i++)
{
// Request the next int from the user,
System.out.print("Please enter the next value: ");
// save it to the ith element of the array
array[i]= scanner.nextInt();
}
//============================================================
// Sort the array's elements in increasing order
//
// Here we will use Selection Sort like algorithm.
Arrays.sort(array);
// The first for loop iterates all elements as element_i
for (int i =0; i < size -1; i++){
array[i]= array[i +1];
}
array[size -1]=0;
// The second for loop finds the right position of element_i
for (int j = i +1; j < size; j++){
// Compare ith value and jth value,
//
//- If array[i]>= array[j], swap these two values
if (array[i]> array[j]){
// For example, let array[i]=10, array[j]=20, to swap
// array[i] and array[j] means array[i] will become 20
// and array[j] will have 10.
//
// To swap the values in two positions, you would need an
// extra variable to temporarily hold the value. For example,
//
// temp = array[i];
// array[i]= array[j];
// array[j]= temp;
//
//-->
int temp = array[i];
array[i]= array[j];
array[j]= temp;
}
}
}
System.out.println();
//============================================================
// Display the sorted array
System.out.print("The array after sorting: ");
for (int i =0; i < size; i++)
{
System.out.print(array[i]+",");
}
System.out.println();
//============================================================
// Remove the minimum in the sorted array
//
// As our array is sorted in the increasing order, to remove the
// minimum, we just remove the first element in the array. It is like
// shifting the array to the left by one element.
//
// Because we are doing shifting, in each iteration, we use two
// values, one at index i, the other at index i+1.
// To prevent us from going over the boundary of array, the following
// offset variable should be set as the correct value.
int offset =0;
// In this for loop, we move the element at i +1 to the position i
for (int i =0; i < size -1; i++)
{
// Move array[i +1] to array[i]
array[i]= array[i +1];
}
// The last element will be set as zero. Remember the greatest index
// should be array.length -1
//-->
array[size -1]=0;
//============================================================
// Display the Array after removing the first element
System.out.print("The array with the minimum removed: ");
for (int i =0; i < size; i++)
{
System.out.print(array[i]+",");
}
System.out.println();
System.out.println();
//============================================================
// Search for an element and remove it
// Ask the user which element to remove
// Use your Scanner and the int variable valueToRemove to get a value for search
System.out.print("Enter the value to search and remove: ");
int valueToRemove = scanner.nextInt();

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Managing Your Information How To Design And Create A Textual Database On Your Microcomputer

Authors: Tenopir, Carol, Lundeen, Gerald

1st Edition

1555700233, 9781555700232

Students also viewed these Databases questions

Question

What has been your desire for leadership in CVS Health?

Answered: 1 week ago

Question

Question 5) Let n = N and Y Answered: 1 week ago

Answered: 1 week ago