The program to take input two array and arrange them in increasing order and finding the difference of 2nd largest and 2nd smallest number is as follows :
********************************************************************************************************************************************
import java.util.*; //Import to declare Scanner variable to take
user input
public class SecondSmallLarge
{
public static void main(String[] args) //Main Function
Starts Here
{
Scanner sc = new
Scanner(System.in); //Declaring Scanner Class to take input form
user
System.out.println("Enter the Size
of Array of Integers");//Prompting user to provide the size of
integer array
int size1 = sc.nextInt(); //Taking
input the size of array of integers
System.out.println("Enter "+size1+"
elements of Integer Array");//Prompting user to provide the
elements of integer array
int arr1[] = new int[size1];
//Declaring integer array arr1 of size given by user
for(int i=0;i<size1;i++)
{
arr1[i]=sc.nextInt(); //Taking input the elements of integer array
from user
}
System.out.println("Enter the Size
of Array of real numbers");//Prompting user to provide the size of
array of type double
int size2 = sc.nextInt(); //Taking
input the size of array of integers
System.out.println("Enter "+size2+"
elements of Array of real numbers");//Prompting user to provide the
elements of array of type double
double arr2[] = new
double[size2];//Declaring array arr2 of type double and size as
given by user
for(int i=0;i<size2;i++)
{
arr2[i]=sc.nextDouble(); //Taking input the elements of array of
type double from user
}
int diff1 = sort1(size1,arr1);//Calling method sort1 and storing
the returned value from the in variable diff1
double diff2=sort2(size2,arr2);//Calling method sort2 and storing
the returned value from the method in variable diff2
//Below line will print the difference of 2nd largest and 2nd
smallest Integers in Integer array
System.out.println("The difference of 2nd largest and 2nd smallest
Integers in Integer array is : "+diff1);
//Below line will print the difference of 2nd largest and 2nd
smallest numbers in array of type double
System.out.println("The difference of 2nd largest and 2nd smallest
numbers in array of type double is : "+diff2);
}
//Method Sort1() takes the size and array of integers as input and
arranges
//the array in increasing order and finds the 2nd largest and 2nd
smallest
//numbers in the array and returns the difference of the numbers to
the main method
//Here we have used selection sort to sort the array
public static int sort1(int n,int arr[]) //sort1()
method starts here
{
int min=0; //variable min is declared which will hold
the index of minimum number in every pass
//selection sort logic starts here
for(int i=0;i<n-1;i++)
{
min = i; //variable min will hold
the index pointed by variable i
for(int j=i+1;j<n;j++)
{
//This loop
scans the array and finds the smallest element and
//the index of
smallest element is assigned to vairable min
if(arr[min]>arr[j])
min=j;
}
//Here swapping is done to the
elements pointed by i and min
//After this swapping smallest
element searched by inner loop is
//places in the index pointed by
i
int temp = arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
//The next for will display the array arranged in
increasing order
System.out.println("Arranged array in Increasing Order
is as follows ");
for(int i=0;i<n;i++)
System.out.println(arr[i]);
//Finding the 2nd smallest element in arranged
array
int secondsmall = arr[1];
//Finding the 2nd largest element in arranged
array
int secondlarge = arr[n-2];
//Printing the 2nd smallest element in the array
System.out.println("Second smallest Number in Integer
Array is : "+secondsmall);
//Printing the 2nd largest element in the array
System.out.println("Second largest Number in Integer
Array is : "+secondlarge);
//The following statement will return the
difference of
//Second largest and 2nd Smallest number to the main
method
return (secondlarge-secondsmall);
}
//Method Sort2() takes the size and array of type
double as input and arranges
//the array in increasing order and finds the 2nd
largest and 2nd smallest
//numbers in the array and returns the difference of
the numbers to the main method
//Here we have used selection sort to sort the
array
public static double sort2(int n,double arr[])
{
int min=0; //variable min is declared which will hold
the index of minimum number in every pass
//selection sort logic starts here
for(int i=0;i<n-1;i++)
{
min = i;//variable min will hold
the index pointed by variable i
for(int j=i+1;j<n;j++)
{
//This loop
scans the array and finds the smallest element and
//the index of
smallest element is assigned to variable min
if(arr[min]>arr[j])
min=j;
}
//Here swapping is done to the
elements pointed by i and min
//After this swapping smallest
element searched by inner loop is places in the index pointed by
i
double temp = arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
//The next for will display the array arranged in
increasing order
System.out.println("Arranged array in Increasing Order
is as follows ");
for(int i=0;i<n;i++)
System.out.println(arr[i]);
//Finding the 2nd smallest element in arranged
array
double secondsmall = arr[1];
//Finding the 2nd largest element in arranged
array
double secondlarge = arr[n-2];
//Printing the 2nd smallest element in the array
System.out.println("Second smallest Real Number is :
"+secondsmall);
//Printing the 2nd largest element in the array
System.out.println("Second largest Real Number is :
"+secondlarge);
//The following return statement will
return the difference of
//Second largest and 2nd Smallest number to the main
method
return (secondlarge-secondsmall);
}
}
***********************************************************************************************************************************************
The Screen shot of the program is as follows
**********************************************************************************************************************************************
The output of the program is as follows
********************************************************************************************************************************************
Enter the Size of Array of Integers
6
Enter 6 elements of Integer Array
34
54
12
33
7
45
Enter the Size of Array of real numbers
5
Enter 5 elements of Array of real numbers
23.5
66.2
8.76
45.7
33
Arranged array in Increasing Order is as follows
7
12
33
34
45
54
Second smallest Number in Integer Array is : 12
Second largest Number in Integer Array is : 45
Arranged array in Increasing Order is as follows
8.76
23.5
33.0
45.7
66.2
Second smallest Real Number is : 23.5
Second largest Real Number is : 45.7
The difference of 2nd largest and 2nd smallest Integers in Integer
array is : 33
The difference of 2nd largest and 2nd smallest numbers in array of
type double is : 22.200000000000003
***********************************************************************************************************************************************
The screen shot of the output is as follows
********************************************************************************************************************************************
Q2. Write programs in Java as follows: a. Get user input for the size and eleme...
Create a program in java to find the 3rd largest and 3rd smallest element in a single dimension array (Input 10 elements) after arranging them in ascending and descending order separately.
Q1. Write a program in Java a. Create 2 separate arrays, of user defined values, of same size b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....
Using Java
In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...
write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...
mathlab
Q2. Write a script file that accepts the user input of array of any size then uses nested for loops to find the minimum and maximum elements in the array. See slides 43,44 & 45 for hints Use the following 2 arrays to test your script: A- A 10 10 -4 17 2 5 0 13 -7 9 AND لیا M -1 5 -7 4 7 17 6 9 10 Sample Outputs: For the given array, the min. value...
in java PART ONE ================================================== Write a program that will read employee earnings data from an external file and then sort and display that data. Copy and paste the input file data below into an external file, which your program will then read. You will want to use parallel arrays for this program. Modify the select sort algorithm to receive both arrays as parameters as well as the size of the arrays. Use the algorithm to sort your earnings array....
Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...
Create two java programs. The first will ask the user to enter three integers. Once the three integers are entered, ask the user to enter one of three functions. The “average” if they want the average of the three numbers entered, enter “min” if they want the minimum and “max” if they want the maximum. Display the answer. Ask the user if they want to calculate another function for the three numbers. If so, loop and ask what function they...
Make a program using Java that asks the user to input an integer
"size". That integer makes and prints out an evenly spaced, size by
size 2D array (ex: 7 should make an index of 0-6 for col and rows).
The array must be filled with random positive integers less than
100. Then, using recursion, find a "peak" and print out its number
and location. (A peak is basically a number that is bigger than all
of its "neighbors" (above,...
JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...