0

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?

2
  • There's a comment in pseudocode: //You are not restricted to a single parameter – use as many as you need. Why not make use of it by adding second parameter, say index of element you want to print, if this index is >= size of array you return ( recursion stop condition) , otherwise you print this single element of you array and call printArr() passing as arguments array and next index ( index + 1 ). This way by starting from index = 0 in first call, you'll recursively print all array one by one. It's an obvious exercise so please don't expect full blown code solution, that's not the point. Commented Aug 4, 2021 at 23:44
  • Unfortunately I am incredibly slow when it comes to learning java. I have tried what you asked, but I'm afraid either I misunderstood or I am simply doing it wrong, as I either get the same results or, in one instance, a build failure. I'm sorry :/ I wish I were better at this, but thanks for your suggestion. I will say that when I have a second paremeter (n), change the return to "n--" and remove the "else", it runs properly, but I think I need the "else".... Commented Aug 5, 2021 at 1:36

1 Answer 1

1

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.