211k views
2 votes
Create a static method called fillArray, which takes an integer array as an input parameter, along with an integer initial value. It should then fill the array with values starting from the initial value, and counting up by 1s. For example, if the data array has length 5, and if the initialValue is 3, then after execution the array should hold this sequence: 3 4 5 6 7. public static void fillArray (int[] data, int initialValue) {

1 Answer

2 votes

Answer:

public class print{

public static void fillArray(int[] arr, int initialValue){

int n = arr.length;

for(int i=0;i<n;i++){

arr[i] = initialValue++;

}

for(int i=0;i<n;i++){

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

}

}

public static void main(String []args){

int[] array = new int[5];

int initialValue =3;

fillArray(array,initialValue);

}

}

Step-by-step explanation:

Create the function with two parameter first is array and second is integer.

Then, declare the variable and store the size of array.

Take the for and fill the array from the incremented value of initialValue by 1 at every run of loop.

After loop, print the element of the array.

Create the main function which is used for calling the function and also declare the array with size 5 and initialValue with 3. After that, call the function with this argument.

User Anttud
by
6.0k points