Question & Answer: Write a recursive method printArray() that displays all the elements in an array of integers, separated by spaces. The arr…..

Write a recursive method printArray() that displays all the elements in an array of integers, separated by spaces. The array must be 100 elements in size and filled using a for loop and a random number generator. The pseudo-code for this is as follows:

//Instantiate an integer array of size 100

//fill the array

For (int i=0; i<array.length; i++)

Array[i] = random number between 1 and 100 inclusive

printArray(integer array);

code must work in netbeans

Expert Answer

RecursiceArray.java

/**
*
* @author Rand0mb0t
*/

import java.util.Random;

class RecursiveArray{
void printArray(int[] arr, int pos){
System.out.print(arr[pos]+” “);
if(pos==arr.length-1){ // The sentinel condition
return;
}
else{
printArray(arr, pos+1); // Recursive call
}
}

public static void main(String argsp[]){

int[] myarray = new int[100];
Random rand = new Random();
for (int i=0;i<myarray.length; i++){
myarray[i]=rand.nextInt(100) + 1;
}
RecursiveArray ra = new RecursiveArray();
ra.printArray(myarray,0);
}
}

OUTPUT:

Question & Answer: Write a recursive method printArray() that displays all the elements in an array of integers, separated by spaces. The arr..... 1

Still stressed from student homework?
Get quality assistance from academic writers!