Question

In: Computer Science

Maximum value and its index in an array java program that: Receive as input in command...

Maximum value and its index in an array

java program that:

  • Receive as input in command line the size of an array (n)
  • Generate an array with numbers between 1 and 100 (inclusive 1 and 100) with the given size (a)
  • Print the array with the format: {a, b, c, ..., x}, for instance {1, 2}
  • Compute and print in the console the following:
    • maximum value in the array
    • one index on which the maximum value appears (a number from 0 to n-1)

Solutions

Expert Solution

Given below is the code for the question. Please do rate the answer if it helped. Thank you.
import java.util.Random;
import java.util.Scanner;

public class FindMax {
   public static void main(String[] args) {
       int n;
       if(args.length != 1) {
           System.out.println("No. of elements not specified as command line argument");
           Scanner input = new Scanner(System.in);
           System.out.print("How many elements? ");
           n = input.nextInt();
       }
       else {
           n = Integer.parseInt(args[0]);
       }
      
       int[] arr = new int[n];
       Random rand = new Random();
       for(int i = 0; i < n; i++) {
           arr[i] = 1 + rand.nextInt(100);
       }
      
      
       //find the max and its index
       int maxIndex = 0;
       for(int i = 1; i < n; i++) {
           if(arr[i] > arr[maxIndex]){
               maxIndex = i;
           }
       }
      
       System.out.println("The array elements are ");
       System.out.print("{");
       for(int i = 0; i < n; i++) {
           if(i != 0)
               System.out.print(", ");
           System.out.print(arr[i]);
       }
       System.out.println("}");
      
       System.out.println("The max element is " + arr[maxIndex] + " at index " + maxIndex);
   }
}


Related Solutions

Define what an array index value is in JAVA. Then, write a JAVA program that passes...
Define what an array index value is in JAVA. Then, write a JAVA program that passes an array to a method and finds the average value or mean value (add up the numbers in the array and divide by the number of values) of the array and prints it out.
java program: Input and output the following details. Your program can only receive the correct input....
java program: Input and output the following details. Your program can only receive the correct input. (For example: a notification will be given if you not enter Char data type for Name) Name: Ali bin Ahmad Occupation: Technician Age: 30 Hometown: Negeri Sembilan Years of Service: 12 Gender: Male
Write a Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
(in java) Find the maximum value and minimum value in milesTracker. Assign the maximum value to...
(in java) Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program: Min miles: -10 Max miles: 40 given code below (please bold the solution, thank you!) import java.util.Scanner; public class ArraysKeyValue { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_ROWS = 2; final int NUM_COLS = 2; int [][] milesTracker = new int[NUM_ROWS][NUM_COLS]; int...
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Write a java script function that accepts integer array as input, and display a new array...
Write a java script function that accepts integer array as input, and display a new array by performing fol lowing modifications, • The values in odd index positions must be incremented by 1 • The values in even index positions must be decremented by 1. • Assume the array index starts from 0. Input 1: arr = [1,2,3,4] Output 1: arr = [0,3,2,5 it done html and javascript and plz do it in simple way
Input: An array of non-negative integers, where each element in the array represents your maximum jump...
Input: An array of non-negative integers, where each element in the array represents your maximum jump length at that position. Output: A boolean value if you are able to reach the last index starting if you start at the first spot in the array. [Please write a recursion function!] Example 1: Input: [2,4,1,2,4,1] Output: True (Ex. 0 to 1 to 5 or 0 to 2 to 3 to 5) Example 2: Input: [3,2,1,0,4] Output: false (You will always arrive at,...
Input: An array of non-negative integers, where each element in the array represents your maximum jump...
Input: An array of non-negative integers, where each element in the array represents your maximum jump length at that position. Output: A boolean value if you are able to reach the last index starting if you start at the first spot in the array. Example 1: Input: [2,4,1,2,4,1] Output: True (Ex. 0 to 1 to 5 or 0 to 2 to 3 to 5) Example 2: Input: [3,2,1,0,4] Output: false (You will always arrive at, and get stuck at, index...
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT