Question

In: Computer Science

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

Solutions

Expert Solution

SOURCE CODE

import java.util.Random;
public class Main
{
public static void main(String[] args)
{
Random rd = new Random(); // creating Random object
int[] arr = new int[6];
for (int i = 0; i < arr.length; i++)
{
arr[i] = rd.nextInt(); // storing random integers in an array
//System.out.print(arr[i]+"\t"); //remove // added before this print statement to see elements in array
}
System.out.print("\n");
System.out.println("Only the first and last element: "+arr[0]+"\t"+arr[arr.length-1]);
System.out.print("Elements at odd index: ");   
for (int i = 1; i < arr.length; i=i+2) //printing elements at odd index
{
System.out.print(arr[i]+"\t");
}
System.out.print("\n");
System.out.print("Every odd element: ");
for (int i = 0; i < arr.length; i++)
{
if(arr[i]%2!=0) //checking whether the array element is odd
{
System.out.print(arr[i]+"\t");
}
}
System.out.print("\n");
System.out.print("All elements in reverse order: ");
for (int i = arr.length-1; i>=0; i--) //printing array elements in reverse order
{
System.out.print(arr[i]+"\t");
}
}
}

Screenshot of code and output

Explanation

Program starts with initialization of an array with 6 random integers. To get random numbers, makes use of random class and declaring it's object. Using the object initializing array with random numbers.

After that displaying the first and last element of the array using index. Index of the first element is 0 and index of the last element is length of the array-1.

Next displaying elements at odd index in the array. We already know that array index is ranging  from 0 to arraylength-1. A loop ranges from 1 to end of the array is used here because odd index starts from 1. since odd index is alternate, we incrementing the loop counter by 2. Hence we gets the array elements at the odd index.

Then next we want to display the odd elements in the array. For that a loop ranges from starting to end of the array and inside loop checking whether the array element is odd. If it is odd then display that array element.

Finally printing the array elements in the reverse order. For that a loop ranges from the end of the array to starting of the array and inside loop printing each element of the array.


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***** 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);
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
In C, write a program that initializes a 3D array (the exact size doesn't matter) that...
In C, write a program that initializes a 3D array (the exact size doesn't matter) that contain what ever arbitrary integers and print out the array including the elements and use different functions so that the main only contains variables and function calls.
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...
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.
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
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT