113k views
1 vote
create a function named dispAry. This function has 2 arguments, the first one is an integer array and second one is a integer to indicate the size of the array. There will be no return value. In the function body the code should loop through the array that was passed and display its contents to the screen. Lastly write the code that calls the function and passes the salesCount array and the size of the array.

User Benpro
by
7.0k points

1 Answer

5 votes

Answer:

The java program for the given scenario is as follows.

import java.util.Scanner;

import java.lang.*;

public class Main

{

//variable declared static and initialized

static int size=10;

//array declared static

static int[] salesCount = new int[size];

//method to display array

static void dispAry(int[] arr, int len)

{

for(int j=0; j<len; j++)

{

System.out.println(arr[j]);

}

}

public static void main(String[] args) {

//array initialized in for loop

for(int j=0; j<size; j++)

{

salesCount[j]=j*2;

}

//method called to display array

dispAry(salesCount, size);

}

}

OUTPUT

0

2

4

6

8

10

12

14

16

18

Step-by-step explanation:

1. A static integer variable, size, is declared and initialized to 10.

2. A static integer array, salesCount, is declared. This array is initialized inside main().

3. A static method, dispAry(), is defined which takes two parameters: an array and an integer variable indicating size of that array.

4. This method only displays the contents of the array.

5. Inside main() method, the array, salesCount, is initialized and passed as a parameter to the method, dispAry(), along with the integer variable, size.

6. The program can be tested for any value of variable, size, which indicates length of the array.

7. The values in the array are double the index of each element; each index is multiplied by 2 and assigned to the element at that particular index.

8. The program can be tested for any numerical values in the array.

9. Since two methods are required, program is written in java language.

10. Each element of the array are printed on a new line using the method println().

11. All the elements of the array can be printed on the same line, separated by space, using method print().

12. In java, all coding is written inside the class. Only variables can be declared at class level, inside class and outside methods. All the logic is to be placed inside methods.

User Tickled Pink
by
6.9k points