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 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
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:...
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
write a python function that takes a number as input argument, and that tries to determine...
write a python function that takes a number as input argument, and that tries to determine two other integers, root and pwr, such that root**pwr is equal to the integer passed as an argument to the function. Consider that pwr > 1. The function should return the values of root and pwr, as a string in the form root**pwr. For example, when the passed argument is 8, the function should return the string ‘2**3’. When the passed input argument does...
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT