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)
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
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...
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...
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
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 code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT