Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The code below sorts a poset of ints from user input based on divisibility. it is also supposed to print the minimal and maximal elements

The code below sorts a poset of ints from user input based on divisibility. it is also supposed to print the minimal and maximal elements of the poset but i am not sure how to find them. Any help would be appreciated!

package selectionsort; import java.util.Scanner; /** * * @author jharr149 */ public class SelectionSort {

public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter number of positive integers:"); int total = scan.nextInt(); System.out.println("Now enter " + total + " positive integers"); int [] arr = new int[total]; for (int i = 0; i < arr.length; i++) { int input = scan.nextInt(); if(input < 0){ System.out.println("That is not a positive integer, try again."); i--; } else arr[i] = input; } System.out.println("Your array before sort:"); printArr(arr); System.out.println(""); sortArr(arr); System.out.println("After sort:"); printArr(arr); min(arr); max(arr); } public static void sortArr(int[] arr){ int min; int temp; for(int i=0; i < arr.length; i++){ min = i; for(int j = min + 1; j= 1){ min = j; } } temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } } public static void min(int[] arr){ int min; int temp; boolean minimal; for(int i=0; i < arr.length; i++){ minimal = true; min = arr[i]; for(int j = min + 1; j= 1){ minimal = false; } } if(minimal) { System.out.println(min); } } } public static void max(int[] arr){ } public static void printArr(int [] arr){ for(int i = 0; i < arr.length; i++){ System.out.print(arr[i] + " "); } } }

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

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions