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

Minimum value and its position Create a program that: Receive as input in command line the...
Minimum value and its position Create a program that: Receive as input in command line the sizes m and n of a two dimensional array, and minValue and maxValue between which the random numbers will be generated Generate a array with numbers between minValue and maxValue (inclusive) with the given size (mxn) Compute the minimum value in the array and one of the rows and columns on which it appears. Print the array as a table. Print the minimum value...
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.
Twin elements Create a program that: Input: Receive as input in command line the sizes m...
Twin elements Create a program that: Input: Receive as input in command line the sizes m and n of a two dimensional array Receive as input in command line the minValue and maxValue between which the random numbers will be generated for your two dimensional array Generate: Generate a two-dimensional with numbers between minValue and maxValue (inclusive) with the given size (mxn) Compute: Compute the number of elements that have a neighbour (up, down, left or right) with the same...
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).
(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...
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.
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.
In Java, write a program that given an array A[1...n] and a value S finds 0...
In Java, write a program that given an array A[1...n] and a value S finds 0 < i < j < n such that A[i] + A[j] = S
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT