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
Which of the following is considered a benefit of using simulation programs? a. They allow users to experience potentially dange
erica [24]

Answer:

All of the above

Explanation:

7 0
3 years ago
What is the definition of assiduous?
LuckyWell [14K]

Answer:

showing great care and perseverance.

6 0
3 years ago
Read 2 more answers
Changes in the ownership of a file do not change the amount of data that is considered to belong to a user.
professor190 [17]
False because if you edit the ownership of file it will give permissions only with the permissions you can edit file,rename,or delete file.
5 0
3 years ago
Write a public static method diagSum, which takes a 2d array of int values as a parameter, and returns the sum of the elements i
saveliy_v [14]

A 2d array (i.e. 2 dimensional array) represents its elements in rows and columns

<h3>The program in Java</h3>

The method in Java, where comments are used to explain each line is as follows

//This defines the method

public static int diagSum(int[][] myArray) {

    //This initializes sum to 0

    int sum = 0;

    //This iterates through each row of the array

               for (int i = 0; i < myArray.length; i++) {

                   //This calculates the sum of the diagonals

                       sum+=myArray[i][i];

               }

               //This returns the sum

               return sum;

       }

Read more about methods at:

brainly.com/question/15969952

6 0
3 years ago
Question 4
Tcecarenko [31]

Answer: squared ← number * number

Explanation:

4 0
3 years ago
Other questions:
  • Given the lists list1 and list2 that are of the same length, create a new list consisting of the first element of list1 followed
    12·2 answers
  • While creating a sketch, what helps you identify positive and negative space?
    13·1 answer
  • Jane wishes to forward X-Windows traffic to a remote host as well as POP3 traffic. She is worried that adversaries might be moni
    14·1 answer
  • who will follow me on tiktok and like all my videos? if you do ill give branlist and give u a shoutout and you can enter my big
    15·1 answer
  • Organizations should communicate with system users throughout the development of the security program, letting them know that ch
    12·1 answer
  • One tool of Lean that is a container, card, or visual limit that signals when work needs to be done or replenishment needs to be
    13·1 answer
  • 18) What is masking in Flash?
    5·1 answer
  • I need subscribers plz ​
    15·2 answers
  • Which 2 tools are useful to remote employees and coworkers.
    10·1 answer
  • The formula for calculating the amount of interest charged on a loan is:
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!