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
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...
write a program which will prompt an array of 12 integers then print the array as...
write a program which will prompt an array of 12 integers then print the array as an array of 4 columns on 3 rows
Write program to store ten integers in an array and the program will do the following...
Write program to store ten integers in an array and the program will do the following Find the average of even marks Find how many prime integers inserted Note: please solve it by c , not c++
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 a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
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.
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user...
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user inputs) as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair. For example, if an array called list stores the values {7, 2, 8, 9, 4, 15, 7, 1, 9, 10}, then the call of shrink(list) should return a new array containing {9, 17, 19, 8, 19}. The first pair from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT