/* ************************************************************************** * Program name : 024_Fibonacci_series (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/11 - first version * ************************************************************************** * Description : (a) Input an non-negetive integer number * * (b) Print the first n fibonacci numbers * ************************************************************************** */ #include #include #include int getnumber(void); // Note (1) unsigned long fibonacci(int Number); int main() { // part 1 : declaration int Number, Index; Number = getnumber(); cout << "\nThe first " << Number << " element(s) of fibonacci numbers are :\n"; for (Index=1; Index<=Number; Index++) { cout << fibonacci(Index); if (Index!=Number) cout << " , "; // Note (2) } cout << "\n" << endl; system("PAUSE"); return 0; } // End of Main int getnumber(void) // Note (3) { int Number; do { cout << "\nPlease input a non-negative integer number: "; cin >> Number; if (Number<=0) cout << "The number must be greater than zero (o)\n"; cout << "\n"; } while (Number<=0); return Number; // Note (4) }; unsigned long fibonacci(int Number) // Note (3) { if (Number <= 2) return 1; else return fibonacci(Number-1)+fibonacci(Number-2); // Note (5) }; // end of function fibonacci /* Notes (1) The statements "unsigned long fibonacci(int Number);" and "int getnumber (void);" are function declaration statements. To declare a function is to give the data type of the value that the function returns (or void if the function has no return value), its name, and in parentheses the data types of its parameters separateed by commas(or void if the function has no parameter). The standard style of writing function declaration is : return-value-type function-name (parameter-list); ----------------------------------------------------------------------- int getnumber (void); or (); unsigned long fibonacci (int Number); or (int); The word "void" means nothing. The function "sum" has no parameters and returns a value of type int. (2) Use this statement to output a comma after ouput each number unless the last one. (3) The header line of the function body : No semi-colon ( ; ) is placed at the end of line. return-value-type function-name (parameter-list w/ variable name); --------------------------------------------------------- int getnumber (void) unsigned long fibonacci (int Number) Remark : C++ -> int getnumber(void); PASCAL -> PROCEDURE getnumber; C++ -> unsigned long fibonacci(int Number); PASCAL -> FUNCTION fibonacci(Number:real):real; (4) Return a non-negative integer number (Number) to the main program. (5) Return the 'n'th fibonacci number to the main program. */