Question

Write an application in Java which includes an algorithm that takes an array of any size,...

Write an application in Java which includes an algorithm that takes an array of any size, selects the high and low integer from the array of integers with each pass and builds a new array of integers by inserting the high and low selection with each pass. Your output should show the original array of integers and the output of each pass on a new line.

Note: You can assume all integers are positive, but your code must work for an even and odd number of
integers and an array of size from 5 to 30.

Example Output:

Input Array: [ 42 , 24, 7, 13, 36, 52]

Pass 1: [7, 52]

Pass 2: [7, 13, 42, 52]

Pass 3: [7, 13, 24, 36, 42, 52]

Homework Answers

Answer #1

Java Code :

import java.util.*;
import java.io.*;

public class HighLowPass{
public static Scanner s = new Scanner(System.in);
static int tempArray2[] = {};
public static void main(String []args){
System.out.println("Enter the array size");
int size = s.nextInt();
int[] numbers = new int[size];
System.out.println("Enter the array elements");
for(int i=0;i<numbers.length;i++){
numbers[i] = s.nextInt();
}
arrayPass(numbers);
}

public static void arrayPass(int numbers[]){

int[] tempArray = numbers;
tempArray = ascendingArray(tempArray);
  
for(int i=0;i<tempArray.length/2;i++){
lowHighArrayPass(tempArray[i],tempArray[tempArray.length-1-i]);
}
if(tempArray.length%2 != 0 ){
lowHighArrayPass(tempArray[tempArray.length/2],-1);
}
  
}

public static void lowHighArrayPass(int low , int high){
int tempArray[] = {low,high};
tempArray = mergeArrays(tempArray,tempArray2);
  
tempArray = ascendingArray(tempArray);
tempArray2 = tempArray;
System.out.print("Pass : [");
for(int a : tempArray){
if(a!=-1) System.out.print(a+" ");
}
tempArray2 = tempArray;
System.out.print("]\n");
}
public static int[] ascendingArray(int[] tempArray){
for(int i=0;i<tempArray.length;i++){
for(int j=0;j<tempArray.length;j++){
if(tempArray[i]<tempArray[j]){
int temp = tempArray[i];
tempArray[i] = tempArray[j];
tempArray[j] = temp;
}
}
}
return tempArray;
}

public static int[] mergeArrays(int a[], int b[]){
int temp[] = new int[a.length+b.length];
int i=0;
for(int element : a){
temp[i++] = element;
}
for(int element : b){
temp[i++] = element;
}
return temp;
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Write a complete Java application according to the following specifications: The application is built with Ant...
Write a complete Java application according to the following specifications: The application is built with Ant and uses gson-2.8.5.jar (or later), which must be included properly in the NetBeans-exported zip file you submit to Canvas The application has at least the following four source-code files: StudentInfoGsonApp.java, Student.java, StudentList.java, PhoneNumber.java The main method in StudentInfoGsonApp creates the studentInfo String array as follows: String[] studentInfo = {"John, Doe, 3.1, 866-555-1212, Rust;Julia", "Jane, Deere, 3.25, 898-555-1212, swimming;sleeping;dreaming;kayaking", "Sam, Spade, 2.9, 888-555-1212, coffee-drinking;Java;Python"}; creates...
Instructions Write a Java code Your goal is to take N integer inputs from the user...
Instructions Write a Java code Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as input...
Dice Rolling) Write an application to simulate the rolling of two dice. The application should use...
Dice Rolling) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
have a java application need to create an application which is able to do some analysis...
have a java application need to create an application which is able to do some analysis on temperature data stored in a data file. You will be given the “temperatures.dat” data file which contains the data you must analyze. The analysis you’ll need to do is: Total number of data points Find coldest temperature Find warmest temperature Find average temperature Find the frequency of each temperature Find the most frequent temperature Find the least frequent temperature All classes must be...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT