In: Computer Science
1. Write a program that computes the smallest and largest of a set of values that are stored in an array. Ask the user for the set size (and hence the array size). Populate the array with user input. (Java language)
import java.util.Scanner;
public class MinimumOfArray
{
public static void main (String[] args)
{
int min,max;
Scanner sc = new Scanner
(System.in);
System.out.print("Enter Number of
elements: ");
int n=sc.nextInt();
System.out.print("Enter Array
elements: ");
int[] arr=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Array is:
");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
min = findmin(arr);//functions are
called
System.out.println("Smallest is:
"+min);
max = findmax(arr);//functions are
called
System.out.println("Largest is:
"+max);
}//end of main method
public static int findmin(int arr[])
{
int min=arr[0];
for(int
i=0;i<arr.length;i++)
{
if(arr[i]<min)
min=arr[i];
}
return min;
}
public static int findmax(int arr[])
{
int max=arr[0];
for(int
i=0;i<arr.length;i++)
{
if(arr[i]>max)
max=arr[i];
}
return max;
}
}