In: Computer Science
Write a java code that:
1) Takes as an argument an integer number, say N
2) Creates an array of size N
3) Populates the array with random numbers between 0 and 10 * N. This is, the values of the elements of the array should be random numbers between 0 and 10 * N.
4) Finally, the code outputs the index of the smallest element and the index of the largest element in the array
//MinMaxAvg.java import java.util.Random; import java.util.Scanner; public class MinMaxRandomArray { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n; System.out.print("Enter value for N: "); n = sc.nextInt(); Random rand = new Random(); int arr[] = new int[n]; for(int i = 0;i<n;i++) { arr[i] = rand.nextInt(10*n+1); System.out.print(arr[i]+" "); } int minIndex = 0, maxIndex = 0; for(int i = 0;i<n;i++){ if(arr[minIndex] > arr[i]){ minIndex=i; } if(arr[maxIndex] < arr[i]){ maxIndex=i; } } System.out.println("\n\nindex of the smallest element: "+minIndex); System.out.println("index of the largest element: "+maxIndex); } }