Question

In: Computer Science

Write a java code that: 1) Takes as an argument an integer number, say N 2)...

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

Solutions

Expert Solution

//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);
    }
}


Related Solutions

Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.   public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) { } Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N ā€“ 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~āˆš?N/2.
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
For all integers n > 2, show that the number of integer partitions of n in...
For all integers n > 2, show that the number of integer partitions of n in which each part is greater than one is given by p(n)-p(n-1), where p(n) is the number of integer partitions of n.
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write an application that includes a constructor, user input, and operators.
a) Write C code using a struct to hold student information: integer id integer number of...
a) Write C code using a struct to hold student information: integer id integer number of hours taken integer number of hours passed double gpa b) create a variable of the type in #25 and initialize it with some data.
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the...
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the user to think of a number between 0 and nāˆ’1, then makes guesses as to what the number is. After each guess, the program must ask the user if the number is lower, higher, or correct. You must implement the divide-and-conquer algorithm from class. In particular, you should round up when the middle of your range is in between two integers. (For example, if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT