Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Survey of Programming Language Concepts, cosc-3308 Lab/Assignment 4 (Oz programm

ID: 3776216 • Letter: S

Question

Survey of Programming Language Concepts, cosc-3308 Lab/Assignment 4 (Oz programming language)

Exercise 1. (Efficient Recurrence Relations Calculation) At slide 54 of Lecture 7, we have seen a concurrent implementation of classical Fibonacci recurrence. This is: fun {Fib X} if X==0 then 0 elseif X==1 then 1 else thread {Fib X-1} end + {Fib X-2} end end By calling Fib for actual parameter value 6, we get the following execution containing several calls of the same actual parameters. For example, F3, that stands for {Fib 3}, is calculated independently three times (although it provides the same value every time). Write an efficient Oz implementation that is doing a function call for a given actual parameter only once. Consider a more general recurrence relation, e.g.: F0, F1, …, Fm-1 are known as initial values. Fn = g(Fn-1, …, Fn-m), for any n m. For example, Fibonacci recurrence has m=2, g(x, y) = x+y, F0=F1=1.

Explanation / Answer

Answer:

#include<iostream>

using namespace std;

int fibonacci(int input)
{
if((input==1)||(input==0))
{
return(input);
}
else
{
return(fibonacci(input-1)+fibonacci(input-2));
}
}

int main()
{
int input,i=0;
cout<<"Input the number of terms for Fibonacci Series:";
cin>>input;
cout<<" Fibonacci Series is as follows ";

while(i<input)
{
cout<<" "<<fibonacci(i);
i++;
}

return 0;
}