An algorithm for computing the sequence of Fibonacci numbers
An algorithm for computing the sequence of Fibonacci numbers is given by:Fib(0) = 0, Fib(1) = 1, Fib(n) = Fib(n-1) + Fib(n-2), n > 1.
a.Write computer source code in an HLL programming language. Use the code and the example of its execution to calculate Fib(3),to identify and discuss ALL of the following concepts regarding procedure semantics:Procedure definition, declaration;functionbody; local variables;scope of variables; lifetime of variables; arguments; caller; callee; return statement; returned value; activation record;actual parameters; formal parameters; parameter passing mechanisms (which kind is used,in the example);type checking of parameters;
Solution
As here its mentioned source code in HLL programing, i am providing in C++
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{ int n; // variable to hold the number to get fibonacci
int first = 0, second = 1; //variables to calculate the next number in series.
//Here first we will ask user to enter the number
cout<<\"Enter the number for which you want the fibonacci series\"<<endl;
cin>>n;
cout<<\"The fibonacci series of entered number is\"<<endl;
for(i =0;i<n;i++)
{
if(c<=1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout<<next<<endl;
}
}
Let us see the above code for Fib(3)
So here n = 3 //as n holds the number for which fibonacci series is required.
Now we will go to the for loop
it starts from c= 0 to c<3 i.e. for c=0,1,2
1) c= 0
First is if condition , c<=1 true hence next becomes c which means next = 0
Output Screen : The fibonacci series of entered number is
0
2) c=1
if(c<=1) true hence next = 1
OutputScreen
The fibonacci series of entered number is
0
1
2) c= 2
if(c<=1) false so control will go to else loop.
next = first + second which is next = 0+1, next = 1, first becomes = 1, second becoms 1.
Output Screen
The fibonacci series of entered numbers is
0
1
1.

