Dodgesmiley
Dodgesmiley

Reputation: 47

Display all the elements of the array using recursive function printArray()

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?

Upvotes: 0

Views: 3511

Answers (1)

enzo
enzo

Reputation: 11486

Let's define our recursive function: I'll be using two parameters: arr being the array to be printed and i the index of the current element in this array to be printed:

public static void printArray(int[] arr, int i);

We can make this method private and define a public overload for this method, accepting only the array to be printed and defining the index parameter as being 0, since we want to print the whole array and therefore starting with the first index:

private static void printArray(int[] arr, int i);

public static void printArray(int[] arr) {
    printArray(arr, 0);
}

See the Overloading Methods section from the documentation.


A recursive function needs

  1. a stop condition - in this case, if the current element is equal to the array length. Since an array doesn't have a index that's equal to its length, we do nothing in this case:
private static void printArray(int[] arr, int i) {
    if (i == arr.length) return;
}
  1. to call itself - in this case, to print the next element. We can do this by passing the next index:
private static void printArray(int[] arr, int i) {
    if (i == arr.length) return;
    printArray(arr, i+1);
}

We also need some logic to the current element (i.e. display it):

private static void printArray(int[] arr, int i) {
    if (i == arr.length) return;
    System.out.println(arr[i]);
    printArray(arr, i+1);
}

Sample code:

class Solutin {
    private static void printArray(int[] arr, int i) {
        if (i == arr.length) return;
        System.out.println(arr[i]);
        printArray(arr, i+1);
    }

    public static void printArray(int[] arr) {
        printArray(arr, 0);
    }

    public static void main(String[] args) {
        printArray(new int[]{});                 // Empty array, outputs nothing
        printArray(new int[]{1});                // Singleton array, outputs 1
        printArray(new int[]{1, 2});             // Array with 2 elements, outputs 1, 2
        printArray(new int[]{3, 1, 4, 1, 5});    // Array with 3+ elements, outputs 3, 1, 4, 1, 5
    }
}

Upvotes: 1

Related Questions