Lesson 3 (Exercise 1)

Fibonacci number generator

From CS100 Summer Homework

文档内容

The Fibonacci sequence of numbers is as follows:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

It is easily obtained by starting the sequence with the numbers 0 and 1, and then setting each subsequent number as the sum of the two previous ones. The sequence has an interesting application, as a proper arrangement of a sequence of squares of which the side lengths are given by the numbers in the Fibonacci sequence may serve as an approximation of the golden spiral.

image

[source: https://en.wikipedia.org/wiki/Fibonacci_number]

The task of this problem is straightforward: Implement a Fibonacci number-­‐generator program. It should simply print the first 90 numbers of the Fibonacci number sequence, as follows:

The next number is 0
The next number is 1
The next number is 1
…

You will need to use a special control statement, which is a for-­‐loop. It has the syntax

for( int i = 0; i < someNumber; i++ )
  { statements …
}

"i" is a variable used as a loop index, and the compound expression in parentheses right after the keyword "for" contains three expression statements. The first one initializes (and possibly even declares) the loop index variable. The second expression is used as a condition to decide whether or not more loops need to be executed (the condition will be checked at the beginning of every loop, including the very first one). The third expression will be evaluated at the end of each loop and is used to update the loop index variable. Note that the loop index variable may of course be used in the compound statement following the for-­‐ loop header.

In order to print custom numbers to the console, you may insert special format specifiers in the string literal to be printed using printf. For example, if you want to print two integers to the console, you can use

printf("%d %d\n", var1, var2);

%d here are special format specifiers that will be replaced sequentially by the values passed starting from the second parameter to the printf function. Every format specifier is of the form %x, where x is replaced according to the actual format of the data. Examples:

c: character 
s: string
d: integer (int)
u: unsigned integer
f: floating point(float)
ld: integer (long)
…

"\n" is the EOL (End Of Line) character and will cause a line break in the console. Ask yourself how big the number becomes, and what bit-­‐width is required!? The required output can be downloaded from the webpage.

Please follow the instructions on the webpage to login to our online grading system and submit your solution there. Please follow the exact instructions on how to register such that we are able to unambiguously identify you. This is very important as the summer-­‐ homeworks will be graded and already make up for a small part of your overall grade.Please follow the instructions on the webpage to login to our online grading system and submit your solution there. Please follow the exact instructions on how to register such that we are able to unambiguously identify you. This is very important as the summer-­‐ homeworks will be graded and already make up for a small part of your overall grade.

在线测试 (语言:C;时间限制:1s)
当前状态

无提交记录

有话想说?