1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ray Of Light [21]
4 years ago
10

Exercise on Single Dimensional Arrays Declare an array reference variable arrayInt for an array of integers. Create the array of

size 100, and assign it to arrayInt. (2 points) Write a method to populate the array with Random numbers between 1 to 25. Signature of the method is: populateArray( int[] ) which returns nothing. Call this method from main with arrayInt--> populateArray(arrayInt). (2 points) Write a method to print an array. Signature of the method is: printArray( int[] ) which returns nothing. Print maximum of ten(10) numbers on a line (see sample output below). Call this method from main with arrayInt--> printArray(arrayInt). Hint: Use the modulus operator. Any number n % 10 will return a value 0-9. (3 points) Write a method that finds the average of the array elements. Signature of the method is: findAverage( int[] ) which returns the average to the calling program.Call this method from main with arrayInt--> findAverage(arrayInt). github
Computers and Technology
1 answer:
victus00 [196]4 years ago
4 0

Answer:

//Import the Random class to allow for the generation of random numbers

import java.util.Random;

public class SingleDimensionalArray {

public static void main(String[] args) {

 // Declaration of an array reference variable for an array of integers

 int[] arrayInt;

 // Creating an array of size 100 and assigning it to arrayInt

 arrayInt = new int[100];

 // Call the method to populate array

 populateArray(arrayInt);

 // Call the method to print array elements with a maximum of ten numbers on a

 // line

 printArray(arrayInt);

 // Call the method to find the average of the elements in the array

 System.out.println("\nThe average of the array elements is " + findAverage(arrayInt));

}

// Method to populate array with random numbers between 1 and 25

// Since it is not specified, 1 and 25 are not inclusive

public static void populateArray(int[] arr) {

 // Create an object of the Random class to generate the random numbers

 Random random = new Random();

 // Create a loop that cycles 100 times beginning at n=0

 // At every cycle, generate a random number and add it to the array

 for (int n = 0; n < arr.length; n++) {

  arr[n] = random.nextInt(23) + 2;

 }

}

// Method to print the elements in an array

public static void printArray(int[] arr) {

 // Create a loop that cycles through the array.

 // At each cycle, the element in the array corresponding to the pointer (n) of

 // the array is printed.

 // There are going to be 100 elements, maximum of 10 per line.

 // The if statement checks if 10 elements are already printed on a line.

 // If that's the case, a new line is printed for the next set of 10 elements.

 for (int n = 0; n < arr.length; n++) {

  if ((n > 0) && (n % 10 == 0)) {

   System.out.println();

  }

  System.out.print(arr[n] + " ");

 }

}

// Method to calculate the average of the elements in the given array

public static double findAverage(int[] arr) {

 // An integer variable to hold the sum of the elements is declared and

 // initialized to zero.

 int sum = 0;

 // A loop to cycle through the array while cumulatively summing the elements is

 // created.

 for (int n = 0; n < arr.length; n++) {

  sum += arr[n];

 }

 // The average of the elements is calculated using the result from the sum

 // above. The value is being type-cast to get a floating point value of the

 // average. This is the right thing to do as the average of numbers is more

 // likely to be a floating point number.

 // The result of the average is stored in a double variable called 'average'.

 double average = (double) sum / arr.length;

 // The average of the elements is returned from the method

 return average;

}

}

Explanation:

Please download the file attached to this response for a proper formatting of the code. The code contains comments explaining every segment of the code. Carefully go through the comments in the code.

Note: The function printArray() prints all the elements in the array - 10 elements per line. Since there is no sample format specified, I have assumed that the elements are separated just by spaces.

Hope this helps!

Download java
You might be interested in
With which game is the Taito Corporation associated?
gregori [183]
It is a japanese video game publisher known for publishing space invaders

4 0
3 years ago
The valence electron configurations of several atoms are shown. how many bonds can each atom make without hybridization? 2s^2 2p
Pie
Answer: This element needs 3 electrons to complete its octet state

Why?:
since the electrons in 2s2 are already paired they cannot form a bond pair with electrons from other elements without hybridisation however the 3 electrons in 2p3 are unpaired so can form bond pairs with electrons from other elements without hybridisation.

Hope this helps!
3 0
4 years ago
A __________ is software that helps a peripheral device establish communication with its host device.
vladimir2022 [97]

Answer:

Answer is Device Driver

3 0
3 years ago
Write a program, called compute_stats.c, which opens a data file, called numbers.txt, which contains of a list of positive integ
oksano4ka [1.4K]
??????????????????????
7 0
3 years ago
What does the poster exemplify?
zloy xaker [14]

Answer:

try D

Explanation:

8 0
3 years ago
Read 2 more answers
Other questions:
  • Is a display, or monitor, considered a piece of computer hardware
    15·1 answer
  • Which of the following factors will have the greatest impact on your credit score?1. Length of credit history 11. Payment histor
    7·1 answer
  • A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never
    7·1 answer
  • Consider an application that transmits data at a steady rate (for example, the sender generates an N-bit unit of data every k ti
    8·1 answer
  • Intellectual property piracy has gotten a small boost from the increasing availability of counterfeit goods through Internet cha
    14·1 answer
  • The computer virus is simply a.......... a. diseases b.set of computer instrustruction or code c. types of bacteria​
    9·1 answer
  • Write a program with a car's miles/gallon and gas dollars/gallon (both floats) as input, and output the gas cost for 10 miles, 5
    15·1 answer
  • GMI = $4,666.67 Total Monthly Deductions $1,131.00 What is the Net Monthly Income (GMI - Total Monthly Deductions) =
    9·1 answer
  • This feature allows you to adjust your view to see the lower and upper part of a document
    15·2 answers
  • Explain the relationship between one’s point of view and understanding spoken text
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!