I need a recursive algorithm for Outputting an array of char

I need a recursive algorithm for: Outputting an array of characters, c, given the starting and ending indices in java.

Other examples given did not have working code, trying to figure it out.

Solution

Recursive algorithm:

1.rec_Array_print(c,start,end) //values for c,start,end are passed from main program

// c is the character array, start is starting index and end ending index

2. Print a character at index start.

3. check if indices start and end are equal, if not, do the following

              3.1.recursive call for method rec_Array_print(c,start+1,end) //increment start with 1 for next index

Sample program is given bellow in java with one step output

Program:

import java.util.Scanner;

public class RecArray

{

    static void print1(char[] c,int start, int end)

    {

        System.out.println(c[start]);

        if(start!=end)

        {

            print1(c,start+1,end);

       }

    }

    public static void main(String[] args)

    {

        int end,start;

        char c[]={\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\',\'j\'};

        Scanner in=new Scanner(System.in);

        System.out.println(\"enter start and end indices\");

        start=in.nextInt();

        end=in.nextInt();

        print1(c,start,end);

    }

}

Output:

run:

enter start and end indices

2 6

c

d

e

f

g

BUILD SUCCESSFUL (total time: 14 seconds)

in above program i have taken char c[] with length 10 so start and end indices must be between 0 and 10

you may increase length in your program

I need a recursive algorithm for: Outputting an array of characters, c, given the starting and ending indices in java. Other examples given did not have working
I need a recursive algorithm for: Outputting an array of characters, c, given the starting and ending indices in java. Other examples given did not have working

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site