-3

As I was working through practice problems I came across this one where I am not sure how I got the wrong answer. I tried increasing the value of "i" by 1 and printing that value. This should print the output "5 6 7" but for some reason does not. I tried to google my answer but can't phrase the question correctly or get good results.

//assuming that the value of 'i' is 4
public static void test(int i) {

if (i < 8) {
   test(i + 1);
   System.out.print (i  + “ “);
   }
}
David
  • 25
  • 7
  • Move the `Sout` statement one line up. See: https://stackoverflow.com/questions/33923/what-is-tail-recursion – Aniket Sahrawat Jan 31 '21 at 12:47
  • This was a practice problem, so I am not supposed to change the code. – David Jan 31 '21 at 12:49
  • @David which value are you passing i? – dreamcrash Jan 31 '21 at 12:49
  • Sorry, I keep forgetting to do that. – David Jan 31 '21 at 12:51
  • I meant 4!!!! I keep looking at different things. – David Jan 31 '21 at 12:55
  • 1
    Which output do you get? "7 6 5 4" if you cannot change the code then I am not sure I understand what you are really looking forward – dreamcrash Jan 31 '21 at 12:57
  • I agree. The OP seems to be asking how to change what the code outputs without changing the code. Doesn't make sense to me ... – Stephen C Jan 31 '21 at 12:58
  • I'm just asking what the code would output. I don't want to change it in any way. "7 6 5 4" was an answer but I don't understand how that would be it. – David Jan 31 '21 at 13:00
  • @David This is the reason https://stackoverflow.com/questions/21426688/the-difference-between-head-tail-recursion Dig into it. – Aniket Sahrawat Jan 31 '21 at 13:01
  • *This should print the output "5 6 7"* Not as written, it shouldn't. And if you can't change it, that's what it will output. – a guest Jan 31 '21 at 13:30
  • Of course you want to change code. That is how you learn programming. By thinking about code, running code, being surprised, more thinking, changing code, running it again. You real do not need other people here. Just add more print statements to so you can OBSERVE what your code is doing when. – GhostCat Jan 31 '21 at 13:31
  • Yes I know that I should be changing code when programming but for this specific problem I was doing multiple choice practice. There was no way to run this code without copying and pasting, which I did and got my answer, though I still do not understand why. All I wanted to know was why head recursion printed the results in reverse order. Thank you for your commentary. – David Jan 31 '21 at 17:37

2 Answers2

2

You call test before you print the value. If you originally invoke test with 5, the machine does this in pseudocode:

test(5)
test(5 + 1)
test(5 + 1 + 1)
test(5 + 1 + 1 + 1)
print 5 + 1 + 1 // 7
print 5 + 1 // 6
print 5

test is called; but before printing anything, it calls itself again with i + 1. This repeats until i is 8, and then it starts printing in the "most inside" function.

You should print first, and then call test again.

CoderCharmander
  • 1,862
  • 10
  • 18
1

Since you are printing the value of i after the recursive call, the code will wait until the recursive function returns before printing the value. Which cause the number to be printed backward.

To prevent this, you might want to call the print statement before going into the recursive call.

    public static void test(int i) {
        if (i < 8) {
            System.out.print (i  + "");
            test(i + 1);
        }
    }

You can test it with this anyfiddle

Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • Thanks, but this is a practice problem so I can't modify the code. Infinite recursion is a possibility. – David Jan 31 '21 at 12:54
  • 1
    Hint from 30 years of programming: the best practice to learn programming is by changing code. Of course you can change your code and make experiments! – GhostCat Jan 31 '21 at 13:34