Hi all

Could someone please explain this program to me, especially lines 25 and 30. I understand that line 30 is a for loop and I understand how it works. However, I am unclear why it is minusing 3 from n and is n the variable passed from line 14? Line 25, I believe, consists of variables and I understand that they are defined by line 7. However, I am unsure how their values change during the running of the program. I would apprciate any help. Thank you.

Listing 7.15. Solving the nth Fibonacci numberusing iteration.

// Listing 7.15
  // Demonstrates solving the nth
  // Fibonacci number using iteration

  #include <iostream.h>

  typedef unsigned long int ULONG;

  ULONG fib(ULONG position);
  int main()
  {
      ULONG answer, position;
      cout << "Which position? ";
      cin >> position;
      cout << "\n";

      answer = fib(position);
      cout << answer << " is the ";
      cout << position << "th Fibonacci number.\n";
     return 0;
  }

  ULONG fib(ULONG n)
  {
      ULONG minusTwo=1, minusOne=1, answer=2;

      if (n < 3)
          return 1;

      for (n -= 3; n; n--)
      {
          minusTwo = minusOne;
          minusOne = answer;
          answer = minusOne + minusTwo;
      }

      return answer;
}

Recommended Answers

All 3 Replies

ULONG fib(ULONG n) is a function and what is passed to that function is in main known as "position".

The "\n" on line 15 is simply a newline character, try removing the whole cout statement to see the difference.

The typedef on line 7 is just something telling the compiler that anything named ULONG should be the exact same thing as the standard unsigned long, or unsigned long int. Which is a 64 bit unsigned ( >= 0 ) variable.

The ULONG variables that are declared on line 25 are just local variables and they will be gone when the function reaches its final bracket.

On line 25, the three variables are set to 1, 1, and 2 because these are the first three numbers in a fibonacci sequence (not including zero).

Roughly translated, line 30 would read: "initially set n to n - 3, then decrement n by one and execute some code so long as n is greater than zero."

n is initialized to n - 3 because answer is initialized to the 3rd element in the sequence (the value 2 is element 3). From that point on n is only used as a counter so the for loop knows where to stop, so n needs to be offset by 3 initially to return the nth element.

Let F represent the fibonacci sequence. Each F[n] (excluding elements 0 and 1) is equal to F[n-2] + F[n-1]. So starting with the initial values for minusTwo, minusOne, and answer, respectively, they will each take on the following values after each iteration of the for loop:

1,2,3
2,3,5
3,5,8

and so on.

Once n = 0, the nth element of the sequence will be reached, and the algorithm spits out the answer.

On line 25, the three variables are set to 1, 1, and 2 because these are the first three numbers in a fibonacci sequence (not including zero).

Roughly translated, line 30 would read: "initially set n to n - 3, then decrement n by one and execute some code so long as n is greater than zero."

n is initialized to n - 3 because answer is initialized to the 3rd element in the sequence (the value 2 is element 3). From that point on n is only used as a counter so the for loop knows where to stop, so n needs to be offset by 3 initially to return the nth element.

Let F represent the fibonacci sequence. Each F[n] (excluding elements 0 and 1) is equal to F[n-2] + F[n-1]. So starting with the initial values for minusTwo, minusOne, and answer, respectively, they will each take on the following values after each iteration of the for loop:

1,2,3
2,3,5
3,5,8

and so on.

Once n = 0, the nth element of the sequence will be reached, and the algorithm spits out the answer.

THANK YOU, THANK YOU, THANK YOU, THANK YOU xx

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.