Teaching Video content

An array of

Why do we need arrays

1. In order to solve the problem of the storage and use of a large amount of the same type of data 2.

2, array classification

One-dimensional array two-dimensional array multidimensional arrayCopy the code

One dimensional array

Int a[5]={1,2,3,4,5}; 2, show the meaning:

1. A [5] indicates that five consecutive positions (five integer variables) are separated in the memory 2. The 5 in parentheses represents 5 elements (variables) of the same type, 5 consecutive positions 3. Then store 1, 2, 3, 4 and 5 in these 5 positions respectively 4. Their labels (variables) are A [0], A [1], A [2], A [3], A [4] respectively.Copy the code

3. Notes 1*

Allocate storage space for n variables in a row all variables must be of the same data type all variables must occupy the same number of bytes (important)Copy the code

2* Note that 5 in a[5] in the definition represents that only A [4] exists when length is used for variables, and 4 is the subscript

4. Two-dimensional arrays

Int a[2][3]={1,2,3,4,5,6}; 2, show the meaning:

1, set a total of 12 elements (variables) of the same type, 12 memory positions 2, equivalent to 2 rows and 3 columns, i.e. the first one is a row, the last one is a column 3, in order to name: A [0] [0], a [0] [1], a [0] [2] a [1] [0], a [1] [1], a [1] [2] (as a variable, a [I] [j] said the I + 1, j + 1)Copy the code

3. Output from a two-dimensional array (nested with a for loop)

Multidimensional arrays

1. There is no multidimensional array.Because memory is linear and one-dimensional

2. An n-dimensional array is equivalent to a one-dimensional array where each element is an n-1-dimensional array (Commonly known as dolls)

As explained below:

Array initialization problem

1, classification,

Int a[4]={2,3,4,5}; Int a[4]={2}; int a[4]={2}; Uninitialized (all garbage values) such as int a[4]; Int a[4]={0};Copy the code

Note that initialization can only be done when it is defined.

int a[4]; A [4] = {1, 2, 3, 4}; That's the wrong way to write itCopy the code


Why do we need functions

1, avoid repeated operation knock on the same function of the structure code 2, is conducive to modular, enhance readability

What is a function

* Logical: a separate block of code that can perform a particular function independently * Physical: 1, can receive data (or not) 2, can process the data received 3, can process data (or not return) * summary: A function is a tool, it's designed to deal with a lot of these problems and functions are like black boxesCopy the code

Three, format

Function declaration: Return value type Function name (data type parameter,...) T =f (4) that is, the result (the result of t being assigned) is the return value of the defined function. 2, F (4) defines the function directly using the definition function: Return value type Function name (data type parameter,...) {function body (statement sequence;) }Copy the code

Declare functions

1. Tell the compiler that the forthcoming letters represent a function. 2. Tell the compiler about the parameters and return value types of functions that may be represented by several letters. 3. Declaration of user-defined functions: The declaration function * must be preceded by the main function and is equivalent to a statement with a semicolon at the end. 4. Declare library functions: use #include< the name of the folder where the library functions are located > to declare themCopy the code

Define the function

1. Type:

Void function name (datatype parameter,...) {} (no return) 2. Data type function name (data type parameter,...) {} return expression; Void function name (void) {} (no return) 4. (void) {} return expression; ··· (void means unable to receive any data, return means to return the value in the expression)Copy the code

2. Essence: Describe in detail the specific ways in which a function can achieve a particular function

Call a function

Format: function name (argument)

The meaning of the return expression

1, return; Terminates the function. 2. , the result of the expression is returned to the function after the function is terminated. However, if the data type of the expression is inconsistent with the return value type before the function name, the return value type before the function name prevails.

Function parameters and arguments

1. Concept differentiation. Parameters are defined only within the defining function and are valid only within that defining function. Arguments are values in the calling function. The value of the actual parameter is equivalent to that assigned to the parameter

2, note: the same number of positions, one to one, data types must be compatible with each other (e.g., float compatible with int)

Ix. Classification of functions

With and without reference function and function returns a value function and the function has no return value function library and user-defined functions (generally referred to as the common function) general function and the main function * * note that a program is only one main function main function to call the common function, general function can't call the main function can be between common function calls The main function is entrance program and exportsCopy the code

X. Matters needing attention

** When there is no function declaration, the function must be defined before the call, otherwise an error will be reported ** When there is a function declaration, the order does not matter, but should follow the principle of modularity

Scope and storage of variables

  • Classification of 1.

    By scope: global variable and local variable by storage mode: static variable and dynamic variable and register variableCopy the code
  • Definition 2.

    Global variables: variables defined outside a function. Scope: valid from the beginning of the definition to the end of the program. Local variables: variables defined within a function or the parameters of that function are called local variablesCopy the code
  • 3. Note: If the name of a global variable conflicts with that of a local variable, the local variable prevails