hey guys, i have a project due tomorrow on recursion. the task was to write a recursive program that prints the value of fibonacci numbers and the number of calls made. i wrote the program and it returns the value at the end just fine, but i have been unable to put in a counter to keep track of the calls without different errors. can anybody pleasee help? also, the second part of the project involves memoization, making an array and storing the values that have been calculated already. the program will then look up if the values are stored and return them, it too should have a counter. heres the code

import java.util.Scanner;

public class Fib
{
    public static void main(String[] args)
    {
        int fib;
        int n;
        Scanner scan = new Scanner(System.in);
        System.out.println("input a number");
        n = scan.nextInt();
        System.out.println("result: " + calcFib(n) + " calls: ");

    }

    int count = 0;

    public static int calcFib (int n)
          {

               if (n<2)
               {

                  int ans = n;
                  return ans;
                }


               else
               {

                  int ans = calcFib(n-1) + calcFib(n-2);
                  return ans;
                }

           }
}

Recommended Answers

All 9 Replies

either define a global variable count
or pass it to the recursive function by defining it global
or make a separate class who will take care of counter where counter will be static member variable.

umm i need a more specific answer... like an example lol... i just started learning java like a week ago

Your entire code with global variable is .....

import java.util.Scanner;

public class Fib{
    static int ctr;
    public static void main(String[] args){
        int fib;
        int n;
        Scanner scan = new                  Scanner(System.in);
        System.out.println("input a number");
        n = scan.nextInt();
        System.out.println("result: " + calcFib(n) + " calls: " + ctr);

    }
    int count = 0;
    public static int calcFib (int n){
        ctr ++;
        if (n<2){

            int ans = n;
            return ans;
        }else{
            int ans = calcFib(n-1) + calcFib(n-2);
            return ans;
        }
    }
}

ok so i have made the variable ctr static. i put the counter at the end of the if else statements so that it does it at the end but it will not compile.... can someone help me out i'm going crazy trying to debug this this. remember, i'm a novice lol. thanks

import java.util.Scanner;
public class Fib
{
    static int ctr= 0;
    public static void main(String[] args)
    {

        int fib;
        int n;
        Scanner scan = new Scanner(System.in);
        System.out.println("input a number");
        n = scan.nextInt();
        System.out.println("result: " + calcFib(n) + " calls: " + ctr);
    }
    public static int calcFib (int n)
          {
              if (n<2)
               {

                  int ans = n;
                  return ans;
                }


               else
               {

                  int ans = calcFib(n-1) + calcFib(n-2);
                  return ans;
                }
              ctr++;
           }
}

Your increment statement is after the return statements. There is no way that code can be reached, which is I'm sure what the compiler is telling you.

hmm so theres no way that the counter can be incremented at the end of the if else statements? why not? what if i wanted to make it say print a word after those commands, how would i do that

Because you have return statements in both branches of execution (the if and the else blocks), any code after that block is unreachable. If you need code after that block then you would have to restructure it to get the returns out of those blocks.

public int function(){
  int result=0;
  if ( ){
    result= someFunction();
  } else {
    result= someOtherFunction();
  }
  // do more stuff if you have to
  return result;
}

now suppose i want to create an array and have the program store the values it calculates, and keep comparing the values to those stored instead of recalculating them.... how would i go about doing that?

then again same strategy, make a global array or make a class MyArray which will handle all needed operation for u. either u can go for making global object of that class or u can declare static array inside that class.

,,,class stuff is better, coz u'll learn oops through.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.