开发者

Was told the code I have is not Recursion...Looking for Guidance to why not!

It was pointed out to me that the statement below is not recursion. I thought recursion only means that it calls itself until the answer is found. What would make this recursion?

public static double totalDistance(int[] x, int[] y, String[] city, int i){

    double xSub = x[i] - x[i-1];
    double ySub = y[i] - y[i-1];
    double distance = Math.pow(xSub, 2) + Math.pow(ySub, 2);
    distance = Math.round(Math.sqrt(distance));
    System.out.println("Distance From " + city[i] + " to " + city[i-1] + " is " + distance + " miles.");

   if (i == 1){
       return distance;  
   }
   else {
      return distance+totalDistance(x,y,city, i-1);
   }
}

This is entire code below in case anyone is curious to what is going on...

import java.util.Scanner;
class distance {


public static void main(String[] args) {

    System.out.println("Welcome to Travel Bliss Distance Calculator!");
    Scanner input = new Scanner(System.in);
    int[] x = new int[5];
    int[] y = new int[5];
    String[] city = new String[5];


    int i=0;
    for (i=0; i < 5;i++){
        System.out.println("Enter City>>");
        city[i] = input.next();
        System.out.println("Enter X Coordinates>>");
        x[i] = input.nextInt();
        System.out.println("Enter Y Coordinates>>");
        y[i] = input.nextInt();
        System.out.println("You Entered: " + city[i] + " with Coordinates: (" + x[i] + "," + y[i] + ") ");


    }
    i = i-1;
    System.out.println("============================================================");

    System.out.println("Calculating Distance Between: " + city[0] +", " + city[1] + ", " + city[2] + ", " + city[3] + ", " + city[4]+" >>>");
    System.out.println("TOTAL of: "+ totalDistance(x, y, city, i)+ " miles.");

}


public static double totalDistance(int[] x, int[] y, String[] city, int i){

    double xSub = x[i] - x[i-1];
    double ySub = y[i] - y[i-1];
    double distance = Math.pow(xSub, 2) + Math.pow(ySub, 2);
    distance = Math.round(Math.sqrt(distance));
    System.out.println("Distance From " + city[i] + " to " + city[i-1] + " is " + distance + " miles.");

   if (i == 1){
       return dist开发者_开发百科ance;  
   }
   else {
      return distance+totalDistance(x,y,city, i-1);
   }
}
}


The totalDistance(...) function is indeed recursive (since it calls itself).


It is recursion -- what they might have meant (or you misunderstood) is that it's not "tail recursion".

This is a subset of recursion that is very simple to optimize into a simple loop (although, Java does not do that yet). To be tail-recursive, you have to return the result of the recursive call -- in your case, you add to it first.


looks like recursion to me. who told you it wasn't?


It IS recursive. Probably the critic is it follows too closely the iterative (loop) version.

Probably then were expecting that, instead of going through the list from beginning to end, you picked the middle city, calculated the distance from the start to that city and from that city to the end and added that. Each half of the distance would be calculated calling the same function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜