Question

In: Computer Science

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.

Solutions

Expert Solution

public class Main
{
//method to reverse the array
public static void reverse(int arr[], int size)
{
//variable declaration
int temp;
  
//reverse the array
for(int i=0; i<size/2; i++)
{
temp = arr[i];
arr[i] = arr[size-i-1];
arr[size-i-1] = temp;
}
}
  
   public static void main(String[] args)
   {
   //array declaration
       int array[] = new int[10];
      
       //variable declaration and initialization
       int i=0;
      
       //fill the array with even integer
       for(int value=2; value<=20; value++)
       {
       if(value%2==0)
       {
       array[i++] = value;
       }
       }
      
       //display message on the computer screen
       System.out.println("The array is: ");
      
       //display the array element on the computer screen
       for(int k=0; k<10; k++)
       {
       //display the element and one space after that
       System.out.print(array[k]+" ");
       }
      
       //method calling
       reverse(array, 10);
      
       //display the message on the computer screen
       System.out.println("\n\nThe reverse array is: ");
      
       //display the array element on the computer screen
       for(int k=0; k<10; k++)
       {
       //display the element and one space after that
       System.out.print(array[k]+" ");
       }
   }
}

OUTPUT:

The array is:
2 4 6 8 10 12 14 16 18 20

The reverse array is:
20 18 16 14 12 10 8 6 4 2


Related Solutions

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 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
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...
1. Write a Python program that performs the following: 2. Defines an array of integers from...
1. Write a Python program that performs the following: 2. Defines an array of integers from 1 to 10. The numbers should be filled automatically without the need for user inputs 3. Find the sum of the numbers that are divisible by 3 (i.e., when a number is divided by 3, the remainder is zero) 4. Swap the positions of the maximum and minimum elements in the array. First, you need to find the maximum element as shown in the...
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 =...
6. Write a Java program to rotate an array (length 3) of integers in left direction...
6. Write a Java program to rotate an array (length 3) of integers in left direction 7. Write a Java program to store a collection of integers, sort them in ascending order and print the sorted integers.
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT