Write a program to accomplish the following tasks: 1) make a random number betwe
ID: 3760245 • Letter: W
Question
Write a program to accomplish the following tasks: 1) make a random number between 10 and 100; 2) make an array of the size of the random number; 3) fill the array with random integers from 0 to 999; 4) print lines of output for each of these: a) the original members of the array b) every element at an even index based on the original displayed array c) every even element based on the original displayed array d) the reverse of the original array e) the first and last element based on the original displayed array, use the length not a literal number f) swap the first and last element based on the original displayed array g) shift all elements to the right once, move the last element to the first based on the original displayed array h) replace all even elements with the number zero based on the original displayed array i) replace each element except the first and last with the larger of its two neighbors based on the original displayed array j) remove the two middle elements based on the original displayed array k) move all odd elements to the front otherwise preserving the order of the array based on the original displayed array l) display true if the original array is stored in increasing order m) display true if the array contains two adjacent elements that are equal n) compute the alternating sum of all elements in the array, for example: 1 2 3 4 5 would be computed as 1 - 2 + 3 - 4 + 5 ... o) display a bar chart equal to the value of the number in each element, for example: 1 2 3 4 5 would display as: * ** *** **** *****
Explanation / Answer
CODE :
import java.util.Arrays;
import java.util.Random;
public class NumRandom {
public static int generateRandom(int min, int max)
{
Random r = new Random();
int rand = r.nextInt(max-min) + min;
return rand;
}
public static void rightShiftArray( int [] randArr )
{
int lastElt = randArr[randArr.length-1]; // save off first element
// right shift array
for( int index =randArr.length-2; index >= 0 ; index-- )
randArr[index+1] = randArr [index];
// add last element into first position
randArr[0] = lastElt;
System.out.print("Array after Right Shift : ");
for(int i=0;i<randArr.length;i++)
System.out.print(randArr[i]+" ");
System.out.println();
}
public static void main(String[] args)
{
int arrSize=generateRandom(10,101);
int[] randArr=new int[arrSize];
//Adding random no's in array. No.'s are between 0 to 999 inclusive.
for(int i=0;i<arrSize;i++)
{
randArr[i]=generateRandom(0, 1000);
}
System.out.print("Original Array : ");
for(int i=0;i<arrSize;i++)
{
System.out.print(randArr[i]+" ");
}
System.out.println();
System.out.print("Array elements at even Index : ");
for(int i=0;i<arrSize;i=i+2)
{
System.out.print(randArr[i]+" ");
}
System.out.println();
System.out.print("Even Elements in Array : ");
for(int i=0;i<arrSize;i++)
{
if(randArr[i]%2==0)
{
System.out.print(randArr[i]+" ");
}
}
System.out.println();
System.out.print("Reverse of Original Array : ");
for(int i=arrSize-1;i>=0;i--)
{
System.out.print(randArr[i]+" ");
}
System.out.println();
System.out.println("First Element is : "+randArr[0]);
System.out.println("Last Element is : "+randArr[arrSize-1]);
//Swapping first and last element.
int temp=randArr[0];
randArr[0]=randArr[arrSize-1];
randArr[arrSize-1]=temp;
System.out.print("Array After swapping first and last element : ");
for(int i=0;i<arrSize;i++)
{
System.out.print(randArr[i]+" ");
}
System.out.println();
rightShiftArray(randArr);
//Replacing all even element with 0.
for(int i=0;i<arrSize;i++)
{
if(randArr[i]%2==0)
{
randArr[i]=0;
}
}
System.out.print("Array after replacing even Elements with 0 : ");
for(int i=0;i<arrSize;i++)
{
System.out.print(randArr[i]+" ");
}
System.out.println();
}
}
OUTPUT :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.