: Use of arrays to find the lowest and highest array value, differentiate the od
ID: 3713058 • Letter: #
Question
: Use of arrays to find the lowest and highest array value, differentiate the odd and even numbers from a given set of values. Problem Description: Write a program to prompt the user to input at least 5 integers. The integers are stored in an array of ints. The program should output the following: a) The highest value in the array. b) The lowest value in the array. c) Array element(s) that are odd numbers d) Array elements(s) that are even values. Note: The array must consist of a minimum of 5 elements or an error will occur. Sample Output: Enter the number of elements in the array. Please enter at least 5: 52 10 12 1 5 My array of elements: 2 10 12 1 5 The highest value is: 12 The lowest value is: 1 Odd numbers are: 1 5 Even numbers are: 2 10 12 Enter the number of elements in the array. Please enter at least 5: 3 Please run the program again and enter at least 5 elements. • Use the name ArrayAnnalyze for the class name. • Use meaningful labels for all outputs. • Follow the programming standards and conventions
Can you please solve and make sure that it works just like the output and no errors in it thank you.
Explanation / Answer
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class ArrayAnnalyze {
public static int maximum(int[] array)//for finding the highest element in array
{
int t = array[0];
for(int i=1;i < array.length;i++){
if(array[i] > t){
t= array[i];
}
}
return t;
}
public static int minimum(int[] array)//for finding the lowest element in array
{
int t = array[0];
for(int i=1;i < array.length;i++){
if(array[i] < t){
t= array[i];
}
}
return t;
}
public static void main(String[] args) {
int no_of_elements;
Scanner get = new Scanner(System.in);
System.out.println("Enter no. of elements you want in array.Please enter at least 5:");
no_of_elements = get.nextInt();
if(no_of_elements>=5){
int myarray[] = new int[no_of_elements];
for(int i = 0; i < no_of_elements; i++)
{
myarray[i] = get.nextInt();
}
System.out.print("My array of elements:");
for(int i = 0; i < myarray.length; i++)//for printing the numbers in array
{
System.out.print(myarray[i]+" ");
}
System.out.println("");
int max_element = maximum(myarray);
int min_element = minimum(myarray);
System.out.println("The highest value is: "+max_element);
System.out.println("The lowest value is: "+min_element);
System.out.print("Odd numbers are:");
for(int j=0;j<myarray.length;j++) //for finding the odd numbers in array
{
if(myarray[j]%2!=0){
System.out.print(myarray[j]+" ");
}}System.out.println("");
System.out.print("Even numbers are:");
for(int k=0;k<myarray.length;k++)//for finding the even numbers in array
{
if(myarray[k]%2==0){
System.out.print(myarray[k]+" ");
}
}}
else
System.out.print("Please run the program again and enter at least 5 elements.");
}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.