Question

In: Computer Science

Write a method that displays every other element of an array. Write a program that generates...

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];

Solutions

Expert Solution

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

============================================================================================


Related Solutions

Write a MIPS program that multiples every element in an array of size 10 by 4...
Write a MIPS program that multiples every element in an array of size 10 by 4 without using the multiply instruction.
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
Write a program in C++ that generates and displays the first N three digit odd numbers....
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
Write a C++ program that makes a 10x10 array and picks random letters and displays in...
Write a C++ program that makes a 10x10 array and picks random letters and displays in different positions
Write a program in C to perform the following: Generates an array of 10 double random...
Write a program in C to perform the following: Generates an array of 10 double random values between 1.0 and 100.0 – assume these are prices for 10 items that a store sells Generates an array of 10 integer random values between 0 and 200 – assume that these are the number of items/units (first array) sold Generate another array called “itemSale” which would contain the total sale for each item. Calculate the total sale for this store and display...
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT