210k views
0 votes
8.6 LAB: Array of Fibonacci sequence - loop Write a program to populate an array with Fibonacci numbers. The Fibonacci sequence begins with 0 and then 1 , each following number is the sum of the previous two numbers. Ex: 0,1,1,2,3,5,8,13. Assume the size of the array is always at least 1 . Use the '+' button under the Registers display to store the size of an integer array in $t0 and the address of the first element of the array in the memory in $1. Ex: If $t0 and $ t1 are initialized in the simulator as 5 and 5000 , the data memory starting at address 5000 will contain: Note: Use the '+' button under the Registers display to initialize $ t0 and $t1. 448654.2921504.qx3zqy7

1 Answer

5 votes

Final answer:

To create a Fibonacci sequence array in a programming environment, two initial values, 0 and 1, are stored in the array. A loop is then used to fill the remaining array with each subsequent number being the sum of the previous two. The registers $t0 and $t1 control the array size and the memory address of its first element, respectively.

Step-by-step explanation:

A program that populates an array with Fibonacci numbers starts with two pre-defined values: 0 and 1. Then, using a loop, it calculates each subsequent number by adding the two previous numbers in the sequence. To create such a program, the size of the array (representing the number of Fibonacci numbers to generate) is stored in the register $t0, and the address of the first element is stored in $t1. The program will iterate, starting from the third array position, and will populate the array with the calculated Fibonacci numbers.

To generate the sequence, one needs to follow these steps:

  1. Store the first two Fibonacci numbers (0 and 1) in the initial two positions of the array.
  2. Use a loop that starts from the third position and iterates until the end of the array size stored in $t0.
  3. In each iteration, sum the last two numbers in the array and store the result in the current position.
  4. Once the loop is complete, the array will contain the Fibonacci sequence up to the specified size.
User Crg
by
8.4k points