Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write C code for: (with use of stdio.h, math.h libraries, and use of for and while loops) 1 In the 13th century, an Italian scientist

Write C code for: (with use of stdio.h, math.h libraries, and use of for and while loops)

image text in transcribed

image text in transcribed

image text in transcribed

1 In the 13th century, an Italian scientist and mathematician, Leonardo Fibonacci, wanted to analyze the growth of a rabbit population. He invented a way to compute a particular sequence of numbers, the so-called Fibonacci sequence. Even if the use of the Fibonacci sequence to analyze rabbit populations did not end up being quite accurate, the Fibonacci sequence is a very important mathematical object to model natural processes. For example, the dimensions of your cochlea' follow a Fibonacci sequence. Flowers grow their petals according to Fibonacci sequences. The form of a highway exit is often modelled with a Fresnel curve, which obeys a Fibonacci sequence as well. The ratio of two consecutive numbers in the Fibonacci sequence is called the golden ratio, which is an important number in Art and Music. In math and computer science, Fibonacci numbers are used to test certain algorithms, such as the Euclidian ged algorithm. Today, you are going to write a program able to compute a number out of the Fibonacci sequence, as well as the ratio between two consecutive numbers in the sequence. The Fibonacci sequence is defined as follows: fo = 0 fi fi fi-1 +f1-2 Vi > 2. This means: the 0-th number in the sequence is fo = 0. The 1-st number is fi = 1, the 2-nd is f2 = fi + fo = 1+0= 1, the 3-rd is f3 = f2 +f1=1+1 = 2 and so on. Write a program fib.c that does the following: Asks the user to enter an integer n that is at least 2. If the user enters an integer that does not satisfy this condition, the program prompts the user again, until an integer n > 2 is entered. See below for examples. The program then initializes two long long variables fibIM2 to 0 and fibim1 to 1 respectively. The program then uses a loop on i, from 2 to n (both included), to compute fr, by computing fibi out of fibiMl and fibimi and updating these two latter variables. At each loop turn, the program also computes the ratio r; = on a double variable for all values of i, starting at 2. Caution: a cast is needed to get a real-valued ratio. When the loop is finished, the program prints out fn as well as the ratio rn. See below for examples. Run your program for different values of n. What do you observe for the ratio rn as n grows large? These are examples of input and output for this program: Please enter an integer that is at least 2: 7 fib(7) = 13, fib(7) / fib(6) = 1.625000. Please enter an integer that is at least 2: 42 fib(42) = 267914296, fib(42) / fib(41) = 1.618034. Please enter an integer that is at least 2: 1 Sorry. Your input is incorrect. Please try again. Please enter an integer that is at least 2: 0 Sorry. Your input is incorrect. Please try again. Please enter an integer that is at least 2: -1 Sorry. Your input is incorrect. Please try again. Please enter an integer that is at least 2: 2 fib(2) = 1, fib(2) / fib(1) = 1.000000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions