CMPS 290 Programming Assignment Arrays and Recursion – Fibonacci Numbers In this programming assignment you will be working with arrays and multiple function calls, including a recursive function call. The goal of the assignment will be to, using a pre-set array of sequence numbers, calculate the Fibonacci sequence number for each value in the array (see the example at the bottom if this isn’t clear).
Instructions and Requirements:
• Create a program that assembles and runs to find the correct output.
• Name your program arrayFibYourLastName.asm.
• Your program must use recursion to solve for the Fibonacci sequence.
• You will use the data segment to define your array and print statements.
• Your “main” will only be used to send items into your function(s) and to print prompts and results.
• You will need one function to traverse your array and then inside of it call your recursive Fibonacci function.
• Your results should be stored to a second array that you will then print out of. • You must make use of the stackpointer to store the values that you will need across function calls and for any saved values registers.
• We will assume Fibonacci(0) is 0 and Fibonacci(1) is 1.
Hints:
• You will use the .word directive in the data segment to define your input array as word values (see the example from class).
• You will also define .space in order to hold your results. • Remember we are working with words, not bytes, and how that affects your offset pointer.
• Keeping up with the size of your array, which you know since you are pre-defining it, will be important.
• Keeping up with your stack and which values you are placing onto it will be important!
• When working, break it up into pieces and solve them individually instead of taking on the whole problem at once. I recommend starting by printing your main array, then copying that into the result array and printing that, then do the copying in a function, then change the values in the result array in the function, then start work on the fibonacci function. Break everything down into individual, manageable sections so you aren’t trying to take on too much at once.
• As always, start early and ask questions if you are stuck, having trouble, or don’t understand something.
Submission:
• Turn in your completed assignment to Moodle. You must fully submit your assignment, not leave it as a draft. Make sure you are finished with your assignment and that the correct file is uploaded before fully submitting it. If you have made a mistake, let me know and I can revert your submission to a draft, so you can make a correct before the due date. The due date can be found on Moodle.
Sample Output:
(Note: The array used in my program was 1, 2, 5, 8, 12, 15 as seen in the Sequence Number column)
This program will calculate out the Fibonacci number at a given sequence position.
The positions will be provided by an array hardcoded into the program.
Output shown below as:
Sequence Number: Fibonacci Number
1: 1
2: 1
5: 5
8: 21
12: 144
15: 610
-- program is finished running –
“High Level” Example Code (Does not include the “main”): //arrayFib takes in an int array of values to find the Fibonacci number of. //It also takes in a second array to store the results into, and the size of each array. //Assume the size of both are the same. //It then calls the Fib(n) function with the current value from the array as n. //After the result is calculated, it is stored in the result array, i is increased, and // if we have more values we repeat the process. arrayFib(int[] array, int[] resultArray, int size){ int i = 0; while(i < size){ int n = array[i]; resultArray[i] = Fib(n); i+=1; } } //Recursively calculate the fibonacci number for the argument n passed in. //If n is 0, then the fibonacci number is 0. //If n is 2 or less, then the fibonacci number is 1. //Else calculate the value by calling the Fib function with n-1 and then // adding that result to a second call to the function with n-2. Fib(int n){ if(n == 0) return 0; if(n == 1 || n == 2) return 1; else return Fib(n-1) + Fib(n-2); }
Code-
#include <stdio.h>
int fib(int n) // recursive programm for finding the resultant
Fibonacci Number in Nth position
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
int main() {
//code
int arrayFib[]={ 1, 2, 5, 8, 12, 15}; // predefind
array elements
int s=sizeof(arrayFib)/sizeof(arrayFib[0]); // size of
arrayFibrr
int arr[s];
for(int i=0;i<s;i++)
{
arr[i]=fib(arrayFib[i]); // storing the resultant
number in arr
}
for(int i=0;i<s;i++)
{
printf("%d:%d\n",arrayFib[i],arr[i]); // printing the
resulatant array in required format
}
return 0;
}
Output-
1:1 2:1 5:5 8:21 12:144 15:610
Note:- Hope you got some idea after this article.
CMPS 290 Programming Assignment Arrays and Recursion – Fibonacci Numbers In this programming assignment you will...
Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to calculate the Fibonacci Function 1.) Use a recursive function called fib() to calculate the Fibonacci Function for the following values for the variable n. int n = 10; int n = 20; int n = 30; int n = 40; int n = 45; int n = 46; 2.) In addition to calculating and displaying the results, use a "timer" to see how long...
Write the code in java programming language To get some practice with recursion. You can do all this in one driver program. Write a recursive method to compute the result of the Fibonacci sequence: Fibonacci(N) = Fibonacci(N -1) + Fibonacci(N-2) N == 0 is 0 N == 1 is 1 Testing: Display the result for Fibonacci(N) and the number of function calls for N = 2, 5 and 10.
Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on! Example: the next number in the sequence above is 21+34 = 55 Source:...
Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)
use Java please.
The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can generate the next number by adding the two previous Fibonacci numbers together. For this sequence, you will be asked to take an input, denoting how many Fibonacci numbers you want to generate. Call this input upperFibLimit. The longest Fib sequence you should generate is 40 and the shortest you should generate is 1. So,1<upperFibLimit<40 The rule is simple given f(0) 0, f(1) 1 ....
Question 32 (Programming) The Fibonacci sequence of number is defined as: In this question we examine a property of this sequence. a) Write a C function definition with header int fib(int [ ] a, int n) which generates an array, a of the first n Fibonacci numbers. (Hint: You do not have to write this recursively. You just have to generate each array entry from the previous two entries.) b) Two numbers are said to be coprime if they have...
1.Take this recursive Fibonacci implementation and convert it into the caching based version discussed in class. Implement your caching to store a maximum of 5 values. Create 2 variations: one that stores all values and one that only stores even values. Make sure that you don't leave any gaps in your cache — if you have 5 cache entries you must cache 5 unique and valid values. Compare the caching implementations to the recursive implementation using the time utility. How...
Let’s work together to develop a call tree for the execution of the following recursive method. (The method allows us to recursively generate the nth integer in the Fibonacci sequence, although you don’t need to be familiar with that sequence to understand this problem.) public static int fib(int n) { if (n == 0 || n == 1) { return 1; } else { int prev1 = fib(n - 2); int prev2 = fib(n - 1); return prev1 + prev2;...
C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a simple pattern: 1,1, 2, 3, 5, 8, 13, 21, 34, 55, By definition, the first two numbers are 1 and 1, and each subsequent number is the sum of the previous two. For your program, you will ask the user to enter a number, n, and then calculate and display the values of the Fibonacci sequence to the nth position. ==sample output== Enter an...
in C++
6. (20)The Fibonacci sequence is the series of integers 0, 1, 1,2, 3, 5, 8, 13, 21, 34, 55, 89.. 1 See the pattern? Each element in the series is the sum of the preceding two items. There is a recursive formula for calculating the nth number of the sequence (the oth number if Fib(0)-0): 8 Fib(N)-/N, if N 0 or 1 ifN> 1 Fib(N-2) Fib(N-1), a. b. c. Write a recursive version of the function Fibonacci. Write...