A c tutorial written by tutor a few years ago (the third tutorial focuses on recursion and Fibonacci)

Source: Internet
Author: User

C Language Learning (3)


Definition of one-dimensional array: type specifier array name [constant expression]

For example, int A [10];

Note: (1) the naming rules for Array names are the same as those for variable names and follow the naming rules for identifiers.

(2) When defining an array, you must specify the number of arrays, that is, the length of the array.

(3) A variable expression can contain constants and symbolic constants, but cannot contain variables.

Application of One-dimensional array: array name [subscript]

Initialize a one-dimensional array: (1) assign an initial value to an array element when defining an array

Int A [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

(2) assign values to only some elements.

Int A [10] = {0, 1, 2, 3, 4}

(3) When assigning an initial value to all array elements, the length of the array is not specified because the number of data items has been determined.

Int A [] = {1, 2, 4}


Common Format of Function Definition: type identifier function name () {declare some statements}

Function parameters: when defining a function, the variable name in the brackets following the function name is a form parameter. When a function is called in the main function, the parameters in the brackets following the function name are called "actual parameters"

Function call: you must first declare that it complies with the function specifications.


Example 1: A rabbit was born every month from the third month of birth, and a rabbit was born three months later. If all rabbits do not die, what is the total number of rabbits per month? (Problem with Fibonacci)

/***************************** Function: calculate the number of Fibonacci *******************************/# include <stdio. h> void main () {long int F1, F2; int I; F1 = 1; F2 = 1; for (I = 0; I <= 20; I ++) {printf ("% 12ld % 12ld", F1, F2); if (I % 2 = 0) printf ("\ n"); F1 = F1 + F2; f2 = F2 + F1 ;}}


Example 2 solve the problem of Fibonacci using Arrays

/*********************************** Function: use arrays to solve the problem of Fibonacci *********************************** /# include <stdio. hvoid main () {int I; long int f [40] = {1, 1}; for (I = 2; I <= 40; I ++) {f [I] = f [I-2] + F [I-1];} for (I = 0; I <40; I ++) {if (I % 4 = 0) printf ("\ n"); printf ("% 12ld", F [I]);} printf ("\ n ");}


Example 2 solve the problem of Fibonacci using recursive functions

/********************* Function: use recursive function calls to solve the problem of Fibonacci *********************/# include <stdio. h> void main () {long int Fibonacci (int I); long int F; int I; for (I = 1; I <40; I ++) {f = maid (I); if (I % 4 = 0) printf ("\ n"); printf ("% lD", f );}} long int maid (int I) {long int F; if (I = 1 | I = 2) F = 1; else f = maid (I-2) + maid (I-1); Return F ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.