Write a program that initializes an array with ten random integers and then prints out the following:
Please write comments above the piece of code that do each bullet point. Or write pseudocode/algorithm for the program below:
package element;
import java.util.Random;
public class element {
public static void main(String a[])
{
int ar[]=new int[10];
int i,sum=0,sumeven=0,sumodd=0,min,max;
for(i=0;i<10;i++)
{
Random r = new Random();
ar[i] = r.nextInt(1000);
}
System.out.println("Elements are: ");
for(i=0;i<10;i++)
{
System.out.println("At index "+i+": "+ar[i]);
}
System.out.println("Elements at even index are: ");
for(i=0;i<10;i+=2)
{
System.out.println("At index "+i+": "+ar[i]);
}
System.out.println("Elements in reverse order: ");
for(i=9;i>=0;i--)
{
System.out.println("At index "+i+": "+ar[i]);
}
System.out.println("First element: "+ar[0]+"\nlast element: "+ar[9]);
min=ar[0];
max=ar[0];
sum+=ar[0];
for(i=1;i<10;i++)
{
sum+=ar[i];
if(ar[i]>max)
max=ar[i];
if(ar[i]
min=ar[i];
}
System.out.println("Maximum element:"+max+"\nMinimum element: "+min);
System.out.println("Sum of all element = "+sum);
for(i=0;i<10;i++)
{
if(i%2==0)
{
sumeven+=ar[i];
}
else
sumodd+=ar[i];
}
System.out.println("Alternating sum is: "+(sumeven-sumodd));
}
}
import java.util.Random;
public class element {
public static void main(String a[])
{
int ar[]=new int[10]; // declare an array
int i,sum=0,sumeven=0,sumodd=0,min,max; // declaration of required variables
for(i=0;i<10;i++)
{
Random r = new Random(); //create random object
ar[i] = r.nextInt(1000); // creating array of 10 random values
}
System.out.println("Elements are: ");
for(i=0;i<10;i++)
{
System.out.println("At index "+i+": "+ar[i]); // print all elements
}
System.out.println("Elements at even index are: ");
for(i=0;i<10;i+=2)
{
System.out.println("At index "+i+": "+ar[i]); //print even elements
}
System.out.println("Elements in reverse order: ");
for(i=9;i>=0;i--) // reverse loop for reverse printing
{
System.out.println("At index "+i+": "+ar[i]); //print elements in reverse order
}
System.out.println("First element: "+ar[0]+"\nlast element: "+ar[9]);
min=ar[0];
max=ar[0];
sum+=ar[0];
for(i=1;i<10;i++)
{
sum+=ar[i]; // adding each element
if(ar[i]>max)
max=ar[i]; // storing maximum value and updating with iteration
if(ar[i]<min) // storing minimum value and updating with iteration
min=ar[i];
}
System.out.println("Maximum element:"+max+"\nMinimum element: "+min);
System.out.println("Sum of all element = "+sum);
for(i=0;i<10;i++)
{
if(i%2==0)
{
sumeven+=ar[i]; // adding even indexed numbers
}
else
sumodd+=ar[i]; // adding odd indexed numbers
}
System.out.println("Alternating sum is: "+(sumeven-sumodd)); // printing difference
}
}
COMMENTS HAVE BEEN ADDED.
ALGORITHM:
STEP 1: Declare array ar and define all required variables i=0 , sum=0,sumeven=0,sumodd=0,min,max.
STEP 2: define a random object .
STEP 3: set ar[i]= r.nextInt(1000) a random number until 1000 and increment i.
[Repeat Step 3 whilei<10]
STEP 4: Print all elements.
STEP 5: set i=0.
Step 6:print ar[i] and increment i by 2.
[repeat step 5 while i<10]
Step 7: set i=9.
Step 8: print ar[i] and decrement i by 1.
[Repeat step 8 while i>=0]
step 9: print 1st element ar[0] and last element ar[9].
Step 10: set min,max,sum as ar[0] and i=1;
Step 11: add ar[i] to sum.
Step 12: if ar[i]>max , set max as ar[i].
Step 13: if ar[i]<min, set min as ar[i].
Step 14: increment i by 1.
[Repeat steps 11 to 14 while i<10]
step 15: set i=0.
step 16: if i is even, add ar[i] to sumeven.
step 17: if i is odd, add ar[i] to sumodd.
step 18: increment i by 1
[Repeat steps 15 to 18 while i<10]
step 19: subtract sumeven and sumodd and print the difference.
[END]
Write a program that initializes an array with ten random integers and then prints out the...
Use the following code, which initializes an array x with 1000 random integers, to answer the questions a and b. java.util.Random generator = new java.util.Random( );//accessing Random class final int MAX = 1000; int[] x = new int[MAX]; for( int j = 0; j < MAX; j++ ) { x[j] = generator.nextInt( MAX ) + 1; } a. Write partial code to print all array elements of x in reverse order (one array element per line) b. Write partial code...
P 6.1 page 373, Write a program to initialize a list with 10 random integers from 0 to 1000(inclusive) and output the following: (10 points) import random SEED = int(input("Input seed: ")) random.seed(SEED) print("\na. Every element at an even index") print("\nb. Every even element") print("\nc. All elements in reverse order.") print("\nd. Only the first and the last element.")
need help editing or rewriting java code, I have this program
running that creates random numbers and finds min, max, median ect.
from a group of numbers,array. I need to use a data class and a
constructor to run the code instead of how I have it written right
now. this is an example of what i'm being asked
for.
This is my code:
import java.util.Random;
import java.util.Scanner;
public class RandomArray {
// method to find the minimum number in...
JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in); System.out.println("Seed:"); int seed = scanner.nextInt(); System.out.println("Length"); int length = scanner.nextInt(); Random random...
Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing 1)The initialized array. 2)Every element at an even index. 3)Every even element. 4)All elements in reverse order. Requirements (and hints): 1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and...
Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...
Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class ArrayMethods { private int[] values; public ArrayMethods(int[] initialValues) { values = initialValues; } public void swapFirstAndLast() { . . . } public void shiftRight() { . . . } .. . } a. Swap the first and last elements in the array. b. Shift all elements to the right by one...
I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...
Write a java program that declares 10 element array (of type integers), creates and initializes the array, and perform the sum of elements of the array using for loop. public class SumArray { public static void main (String[], args) { } // end of main } // end of SumArray class
1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...