QuestionAsk Questions?

1) write a function minE which takes an array of distinct integers and returns the smallest number. (If this is too easy return the 2nd smallest element.) (If this is still too easy, also pass an int k to minE and return the k smallest element.) 2) write a function rev which takes an array of integers and reverses it. 3) write a function isDistinct which takes an array of integers and returns a boolean, indicating

View Answers

October 10, 2012 at 2:55 PM

Here is an example that accepts the integers from the user and find the smallest and second smallest number from the array. It also reverses the array of numbers.

import java.util.*; public class Smallest { public static void minE(int numbers[]){ int smallest = numbers[0]; int secondSmallest = numbers[1]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] < smallest) { secondSmallest = smallest; smallest = numbers[i]; } } System.out.println("Smallest value: " + smallest); System.out.println("Second Smallest value: " + secondSmallest); } public static void rev(int numbers[]){ System.out.println("Numbers in reverse order: "); for (int i = numbers.length-1; i >=0; i--) { System.out.println(numbers[i]); } } public static void main(String[] args) { Scanner kybd = new Scanner(System.in); int[] numbers = new int[5]; System.out.println("Enter 5 integers: "); for (int i = 0; i < numbers.length; i++) { numbers[i] = kybd.nextInt(); } minE(numbers); rev(numbers); } }

Related Tutorials/Questions & Answers:

Advertisements