Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6624285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:38:02+00:00 2026-05-25T21:38:02+00:00

supposed to reverse an array of strings recursively. having trouble implementing this. if i

  • 0

supposed to reverse an array of strings recursively.

having trouble implementing this. if i was using a for loop i would just start it at the end of the array and print out the array starting with the last element and ending with the first.

I’m not too sure how to do it recursively. i was thinking about using a swap but that idea sort of fizzled when i couldnt figure out how to change the elements that i was swapping.

any ideas or a push in the right direction would be appreciated.


this is what icame up with so far. i know its wrong, i get an error out of bounds exception which im not sure how to fix. I think im not swapping the first and last correctly. but am i getting the right idea?

this is what i came up with.
a is an array. its inside a class.

// reverse an array 

public void rev() 
{
    rev(0,a.length-1);
} 

private void rev(int first, int last)
{
if(last == 0)
{
//do nothing
}

else
{
while(first != last)
{
int temp = first;
first = last;
last = temp;


System.out.print(" " + a[first]);
rev((first + 1), (last - 1));
}

}
}

made some changes and it reverses the last 3 elements but the it repeats the second element. i have no if statement that controls when it runs so shouldnt it run until left = right?

this is what i changed it to

// reverse an array 
public void rev() 
{
    rev(0,a.length-1);
} 

private void rev(int first, int last)
{
    if(last == 0)
    {
    //do nothing
    }

    else
    {


    String temp = a[first];
    a[first] = a[last];
    a[last] = temp;

    System.out.print(" " + a[first]);
    rev(first+ 1, last-1);

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:38:02+00:00Added an answer on May 25, 2026 at 9:38 pm

    the while loop is too much, since you are using recursion anyway, try it like this

    private void rev(int first, int last)
    {
       if(first < last)
       {
          var temp = a[first];
          a[first] = a[last];
          a[last]  = temp;
          rev(first + 1, last - 1);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is supposed to output what kind of shell I'm using, which
Supposed I have the following string: string str = <tag>text</tag>; And I would like
This is supposed to be a no brainer but I still can’t figure it
I am using Qt 4.5 and having Qt Creator as the IDE. I am
I have this python script which is supposed to wrap all that look like
Just a simple program to get used to pointers. The program is supposed to
Say I have an array like this arr = [24, 21, 25, 40, 236,
I'm having a bit of trouble with an iPad app I'm creating. There is
Using this grammar i could find the exponents but the answer I get is
I am creating a three dimensional array like this: GLfloat ***tgrid; //other code in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.