Question

In: Computer Science

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

Solutions

Expert Solution

import java.util.Random;
public class Main
{
public static void main(String[] args)
{

Random rand = new Random(); // create object of Random class to call
int[] arr= new int[10];// create array of size 10
// loop to fill array with random numbers between 0 to 50
for(int i=0; i<arr.length;i++)
arr[i]= rand.nextInt(50);

System.out.println("Every element at an odd index :");
oddIndex(arr);

System.out.println("Every odd element in array :");
oddElements(arr);

System.out.println("\nArray contents in reverse order are :");
reverse(arr);
}

// function to print elements at odd index

static void oddIndex(int arr[])
{
for(int i=1;i<arr.length;i+=2)
System.out.println("index =" + i + "element = " +arr[i]);
}

static void oddElements(int arr[])
{
for(int i=0; i<arr.length;i++)
{
if (arr[i]%2!=0)
System.out.println("index =" + i + "element = " +arr[i]);
}
}

//function to print in reverse

static void reverse(int arr[])
{

for(int i=arr.length-1;i>=0;i--)
System.out.print( "\t" + arr[i] );
}
}


Related Solutions

*****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)
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 Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and produces as output the sum of integers in an array, the largest and the smallest element in an array. Your program should contain the following methods: public static int sum(int[]) public static int findLargest(int[]) public static int findSmallest(int[])
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.
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);
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
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 a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT