Question

In: Computer Science

You are expected to write a program from scratch. In the program, an array will be...

You are expected to write a program from scratch. In the program, an array will be initialized with 23 random integers between 1000 and 1999 (inclusive). The output of the program is 4 lines on the screen, specifically,

  • All elements in the array (line 1)
  • All elements in reverse order (line 2)
  • Every element that is less than 1500 and also at an odd index (line 3)
  • Every odd element that is larger than 1500 (line 4)

Note that you have to use the method Math.random() we covered previously to generate the random numbers. Other ways like using Random class we haven't covered or just reading in 23 numbers from keyboards are NOT accepted.

Please explain/comment on what each line means and what the process is. Thank you, I appreciate it.

Solutions

Expert Solution

Please find the code below::

RandomArray.java

package classes10;

public class RandomArray {
   public static void main(String[] args) {
       //declare array of size 23
       int myArray[] = new int[23];

       //populate array with random data
       for(int i=0;i<23;i++){
           myArray[i] = (int) (Math.random()*1000+999);
       }

       //printing array in right order
       for(int i=0;i<23;i++){
           System.out.print(myArray[i]+" ");
       }

       System.out.println();
       //printing array in reverse order
       for(int i=22;i>=0;i--){
           System.out.print(myArray[i]+" ");
       }


       System.out.println();
       //printing odd index number less than 1500
       for(int i=0;i<23;i++){
           if(i%2==0 && myArray[i]<1500)
               System.out.print(myArray[i]+" ");
       }

       System.out.println();
       //printing odd index number less than 1500
       for(int i=0;i<23;i++){
           if(myArray[i]>1500 && myArray[i]%2!=0)
               System.out.print(myArray[i]+" ");
       }

   }
}

output:


Related Solutions

You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
Write a program that uses an array of doubles initialized to whatever values you wish. Write...
Write a program that uses an array of doubles initialized to whatever values you wish. Write methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same...
Write a program that asks the user to enter an array of 8 characters then you...
Write a program that asks the user to enter an array of 8 characters then you have to check if characters ‘b’ or ‘a’ appears within the characters and replace them with ‘B’ or ‘A’. For example you enter “a m a l a a b d” The output should be “A m A l A A B d”
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT