In: Computer Science
Write a method that displays every other element of an array.
Write a program that generates 100 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, . . . , 9s.)
Write two overloaded methods that return the average of an array with the following headers:
public static int average(int[] intArray)
public static double average(double[] dArray)
Also, write a test program that prompts the user to enter ten double values, invokes the correct method, and displays the average value.
Given this array, write the code to find the smallest value in it:
int[] myList = new myList[n];
Q1: Write a method that displays every other element of an array.
Java code
package shape;
import java.util.Random;
public class array1 {
public static void main(String[] args) {
// TODO Auto-generated method
stub
//array of 20 intergers
int arr[]=new int[20];
Random rand = new Random();
int i;
for(i=0;i<20;i++)
{
arr[i]=rand.nextInt(50) + 1;
}
//display every other element of
array
for(i=0;i<20;i+=2)
{
System.out.println("arr["+i+"] = "+arr[i]);
}
}
}
============================================================================================
Output
============================================================================================
Q2: Write a program that generates 100 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, . . . , 9s.)
Java code
package shape;
import java.util.Random;
public class array1 {
public static void main(String[] args) {
// TODO Auto-generated method
stub
//array of 10 intergers
int arr[]=new int[10];
Random rand = new Random();
int i;
for(i=0;i<100;i++)
{
//generates 100
random integers between 0 and 9
int
temp=rand.nextInt(10);
arr[temp]++;
}
//display array
for(i=0;i<arr.length;i++)
{
System.out.println("arr["+i+"] = "+arr[i]);
}
}
}
============================================================================================
Output
============================================================================================