This is the assignment for my object-oriented java programming class:
Write a recursive function called 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:
//Pseudo-code for main
//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); //You are not restricted to a single parameter – use as many as you need
//Pseudo-code for printArray()
//Base Case
If the array is empty return
//Recursive Case
Else print the next value in the array and then call printArray() to print the next value
this is where I'm running into trouble
I can print all the integers with a space between them like I'm supposed to, but I can't figure out how to call on printArray in order to print it, which is how I'm supposed to display the array.
This is my code so far:
import java.util.*;
public class ArrayPrinter {
public static void main(String [] args){
int Array[] = new int [100];//initialize array
Random RandomNumber = new Random();
System.out.println("Random int array: ");
for(int i = 0; i<100; i++){//fill the array
Array[i] = RandomNumber.nextInt(100 - 1)+ 1;
System.out.print(Array[i] + " ");//this properly prints out 100 random
//numbers between 1 and 100
}
System.out.println();
System.out.println("All array elements: ");
printArray(Array);//but this does nothing. There are no error notifications,
//it's just nothing happens at all.
}
public static void printArray(int [] Array){
if (Array.length > 0) {
int m = Array.length - 100;
return;//according to the psuedocode I need a return
}
else{//and I need to have an if....else
System.out.print(Array + " ");
printArray(Array);
}
}
}
My question:
How do I call on method printArray to print out all the array elements?