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

1 Write a Java method that takes an integer, n, as input and returns a reference...
1 Write a Java method that takes an integer, n, as input and returns a reference to an array of n random doubles between 100.0 and 200.0. Just write the method. 2. You have a class A: public class A { int i, double d; public A(double d) { this.d=d; this.i=10; } }
1. Write a queue client, "LineNum," that takes an integer command line argument “n” and prints...
1. Write a queue client, "LineNum," that takes an integer command line argument “n” and prints the nth string from the first string found on standard input. [MO6.2] Please note that you would need to use a queue to implement it for full credit. You should add the strings inputted by the user to a queue using the enqueue method. Then you should remove and return "n" strings from the queue using the dequeue method. The nth string that is...
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 C program called test that takes one command line argument, an integer N. When...
Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits
Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a...
Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a number of bunnies and return another integer for the total number of ears that the group of bunnies has. (Assume ear bunny has exactly 2 ears).. Write a method countX, that when given a string counts the number of lowercase 'x' chars in the string. countX("xxhixx") → 4 countX("xhixhix") → 3 countX("hi") → 0 Write a method copies that, when given a string and...
Must be written in JAVA Code Write a program that takes a whole number input from...
Must be written in JAVA Code Write a program that takes a whole number input from a user and determines whether it’s prime. If the number is not prime, display its unique prime factors. Remember that a prime number’s factors are only 1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied...
Write a java code that takes 2 parameters input and output and and guesses value of...
Write a java code that takes 2 parameters input and output and and guesses value of 3 variables x,y,z for following equation: output/input = (x)/(y*z) and range of x = 2 to 512 and y and z = 2 to 32 for example public int method(int input, int output) and input was 10 and output was 80 it'll return x=32 and y=2 and z=2.
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
Python: Write a recursive function that accepts an integer argument, n. The function should display n...
Python: Write a recursive function that accepts an integer argument, n. The function should display n lines of asterisks on the screen, with the first line showing 1 asterisk, the second line showing 2 asterisks, up to the nth line which shows n asterisks. Test the function.
Write a function in python that takes in an integer n and computes the left hand...
Write a function in python that takes in an integer n and computes the left hand Riemann sum of the function f(x) = x^2 on the interval [0,1]. Hint: compute the error from the true answer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT