Question

In: Computer Science

Programming Write a program that initializes an array with 10 random integers between 0 and 100...

Programming

Write a program that initializes an array with 10 random integers between 0 and 100 and then prints exactly 5 lines of output. These 5 lines should contain:

  • The elements in the array, as printed by Arrays.toString()
  • Every element at an even index
  • Every even element
  • All elements in reverse order
  • Only the first and last element

Initially write your code so that you initialize the array in the main method. You will not ask the user for any input, but will use Math.random to generate 10 random numbers between 0 and 100 to put in the array.

As an example, if the array contained { 34, 82, 45, 92, 71, 1, 18, 20, 52, 99 }, the five lines would be:

Array: [ 34, 82, 45, 92, 71, 1, 18, 20, 52, 99 ]

Elements at even index: 34 45 71 18 52

Even elements: 34 82 92 18 20 52

Reverse order: 99 52 20 18 1 71 92 45 82 34

First: 34 and Last: 99

If you complete all the steps above, please go back and move your code that fills the array into a separate method within your class. The method will create the array, fill it with the random numbers, and then return it to the caller.

Remember to use good coding style, and javadoc comments where appropriate.

NOTE: Please read instruction and example carefully while writing java code. Please include JAVADOC too. Thank you in advance.

Solutions

Expert Solution

CODE:

import java.util.*;
class randomNumbers {
   public static void main(String[] args) {
       Random r = new Random();
       int minValue = 0, maxValue = 100;
       int randomIntegersArray[] = new int[10];

       for (int i = 0; i < 10; i++)
           // Randomly generating values with range 0 - 100
           randomIntegersArray[i] = r.nextInt(maxValue - minValue) + minValue;

       // Displaying randomIntegersArray using Arrays.toString() method
       System.out.print("Array : " + Arrays.toString(randomIntegersArray));

       // Displaying even indexes element in randomIntegersArray
       System.out.print("\nElements at even index : ");
       for (int i = 0; i < 10; i++) {
           if (i % 2 == 0) {
               System.out.print(randomIntegersArray[i] + " ");
           }
       }

       System.out.print("\nEven elements : ");
       for (int i = 0; i < 10; i++) {
           // Accessing every even element in randomIntegersArray
           if (randomIntegersArray[i] % 2 == 0) {
               System.out.print(randomIntegersArray[i] + " ");
           }
       }

       // Displaying elements in reverse order of randomIntegersArray elements
       System.out.print("\nReverse order: ");
       for (int i = 9; i >= 0; i--)
           // Accessing every element in reverse order of randomIntegersArray
           System.out.print(randomIntegersArray[i] + " ");      

       // Displaying first and last elements in randomIntegersArray
      
       System.out.println("\nFirst : " + randomIntegersArray[0] + " and Last :"
               + randomIntegersArray[9]);
   }
}

Screenshot of Code & Output:


Related Solutions

Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
IN JAVA PLEASE!! Write a program that initializes an array with ten random integers and then...
IN JAVA PLEASE!! Write a program that initializes an array with ten random integers and then prints four lines of output, containing •Every element at an even index. •Every even element. •All elements in reverse order. •Only the first and last element.
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input...
Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13 count: 5 pid: 13342...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime =...
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT