Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

C++ Check the Control Structures-Repetition Il slide in Week 5. Implement the Fibonacci Number example presented in pages 20 through 30. % 0 of 2

C++

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Check the Control Structures-Repetition Il slide in Week 5. Implement the Fibonacci Number example presented in pages 20 through 30. % 0 of 2 topics complete Lab 6 2 Assignment Due March 2 at 12:30 AM Ends Mar 6, 2020 12:30 AM Compile and execute the program making sure that all syntax and logic errors are removed. Name the CPP file LastNameFirstInitial.cpp. For example, I would name my file LeeD.cpp. Upload a file for this assignment. Explain your program appropriately using comments (/**/, //) inside the CPP file. Programming Example: Fibonacci Number Consider the following sequence of numbers: - 1, 1, 2, 3, 5, 8, 13, 21, 34, Called the Fibonacci sequence . Given the first two numbers of the sequence (say, al and a2) - nth number an. n >= 3, of this sequence is given by: a = a + an-2 Programming Example: Fibonacci Number (cont'd.) Fibonacci sequence -nth Fibonacci number -a - 1 -a = 1 - Determine the nth number an >= 3 Programming Example: Fibonacci Number (cont'd.) Suppose az = 6 and a = 3 -ay-a, + a, = 6 + 3 = 9 -a, -as + az = 9 + 6 = 15 Write a program that determines the nth Fibonacci number, given the first two numbers Programming Example: Input and Output Input: first two Fibonacci numbers and the desired Fibonacci number Output: nth Fibonacci number Programming Example: Problem Analysis and Algorithm Design Algorithm: - Get the first two Fibonacci numbers - Get the desired Fibonacci number Get the position, n, of the number in the sequence - Calculate the next Fibonacci number Add the previous two elements of the sequence - Repeat Step 3 until the nth Fibonacci number is found - Output the nth Fibonacci number Programming Example: Variables int previous1; //variable to store the first Fibonacci number int previous2; //variable to store the second Fibonacci number int current; //variable to store the current //Fibonacci number int counter; //loop control variable int nthPibonacci; //variable to store the desired //Fibonacci number Programming Example: Main Algorithm Prompt the user for the first two numbers- that is, previousl and previous2 Read (input) the first two numbers into previousl and previous2 Output the first two Fibonacci numbers Prompt the user for the position of the desired Fibonacci number Programming Example: Main Algorithm (cont'd.) Read the position of the desired Fibonacci number into nth Fibonacci - if (nthFibonacci == 1) The desired Fibonacci number is the first Fibonacci number, copy the value of previousl into current -else if (nthFibonacci == 2) The desired Fibonacci number is the second Fibonacci number; copy the value of previous 2 into current Programming Example: Main Algorithm (cont'd.) -else calculate the desired Fibonacci number as follows: Start by determining the third Fibonacci number Initialize counter to 3 to keep track of the calculated Fibonacci numbers. Calculate the next Fibonacci number, as follows: current = previous2 + previous1; Programming Example: Main Algorithm (cont'd.) - (cont'd.) Assign the value of previous2 to previous1 Assign the value of current to previous2 Increment counter Repeat until Fibonacci number is calculated: while (counter = nthFibonacci) current = previous2 + previousl; previousl = previous2; previous2 = current; counter++; Output the nthFibonacci number, which is current

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions