Dark Horse programmers -- IOS study notes -- array and sorting

Source: Internet
Author: User
Tags array definition

Dark Horse programmers -- IOS study notes -- array and sorting
IOS Study Notes

Overview:

8_2. symbol for changing Integer Variables
8_2. Change the storage space occupied by Integer Variables
Storage of 8_3 and char data
8_4. Basic concepts and categories of Arrays
8_5. array elements are used as function parameters.
8_5. Definition of one-dimensional arrays and precautions
8_6, one-dimensional array Initialization
8_7, one-dimensional array is a confusing problem
8_8. One-dimensional array reference
8_9. Application: array Traversal
8_10. Storage of one-dimensional arrays
8_11. address of one-dimensional array
8_12, one-dimensional array Length Calculation Method
8_13. Out-of-bounds of one-dimensional arrays
8_14. Application: Find the maximum value
8_15. array elements are used as function parameters.
8_16. array name as function parameter
8_17. Notes about using array names as function parameters
8_17. Notes for using array names as function parameters-Supplement
8_18 idea of Bubble Sorting
8_18. Idea of Bubble Sorting-Supplement
8_19. Bubble Sorting code implementation


Integer variable modifier introduction (modifiers include: int short long [long] signed unsigned)


1. Change the storage space occupied by Integer Variables
% Hd short int a = 4; --> occupies two bytes of printf ("% hd \ n", variable name );
Example: short int a = 1;
Short int a1 = a <15; // Let a shift left by 15 digits, minimum negative number
A1 = (a <15)-1; // shifts left by 15 digits, the largest positive number.
Printf ("d % \ n", a1 );
The short-modified int occupies 16 bytes.
The range is:
10000000 00000000 --> minimum negative number-32768
01111111 11111111 --> the maximum positive number is 32767.


Int 4-byte output with % d or % I --> occupies four bytes
For example, int B = 1;
Int b1 = B <31;
B1 = (B <31)-1; // 31 digits left
Printf ("d % \ n", b1 );
The range is:
10000000 00000000 00000000 00000000 --> minimum negative number-2147483648
01111111 11111111 11111111 11111111 --> the maximum positive number is 2147483647.


Long 8 bytes output with % ld --> occupies eight bytes
Example: long int c = 1;
Long int c1 = c <63; // 63 digits left shift
C1 = (c <63)-1;
Printf ("c1 = ld % \ n", c1 );
The range is:
10000000 00000000 00000000 00000000... 00000000 (64-bit in total) --> minimum negative-9223372036854775808
01111111 11111111 11111111 11111111... 11111111 (64-bit in total) --> the maximum positive number is 9223372036854775807.


Long 8 bytes Output Using lld % (mainly to solve the 32-bit machine problem) --> occupies eight bytes
Note: All modifiers are used to modify integer variables.
* Long TYPE
In a 32-bit system:
Long occupies 4 bytes (storage space for changing integer variables)
Long occupies 8 bytes
In a 64-bit system:
Long occupies 8 bytes
Long occupies 8 bytes


2. Change the integer variable symbol.
Signed indicates that it is signed (the computer is signed by default)
Unsigned indicates an unsigned number (the value range of a positive number is doubled when the integer variable is changed .) Output Using u % printf ("u % \ n", variable name );






3. char Data Storage
1) storage of char Constants
Sizeof ('A ')
--> First find the ASCII value of 'A' --> 97
--> Set 97 to int type --> 00000000 00000000 00000000 01100001
Char ch = 'A'
--> 97
--> Convert 97 to a binary byte of 01100001
--> Store one byte in memory
Summary: char constants and variables are stored differently. (Note: In the Xcode environment. If it may be the same in other environments)
The range of one byte: 0--127 uses ASCII code for common characters




4. Basic concepts and categories of Arrays
1) Basic concepts:
In programming, for the convenience of data processing, we organize several variables of the same type in an orderly manner. These collections of similar data elements in order are called arrays.
In C, arrays belong to the constructed data type. An array can be divided into multiple array elements, which can be basic or constructor types.
2) several terms of the array:
What is an array: an ordered set of data with the same data type.
Array element: data that forms an array. Each array element in the array has the same name and different subscript, which can be used as a single variable. Therefore, it is also called a subscript variable.
Subscript of an array: an index or indicator of the position of an array element.
Array dimension: the number of objects under the array. The dimension of the data array can be divided into one-dimensional array, two-dimensional array, three-dimensional array, and multi-dimensional array.


Sort by storage content: arrays are classified:
Numeric array: used to store Arrays
Character array: used to store characters
Pointer array: used to store pointers (addresses)
Structure array: used to store data of a struct type.
Classification by dimension:
One-dimensional array:
Two-dimensional array:
3D array:
Multi-dimensional array:


5. array elements as function parameters
1) arrays can be used as function parameters for data transmission.
A. Use array elements (subscript variables) as real parameters;
An array element is a subscript variable, which is no different from a common variable. Therefore, it is used as a function real parameter and is identical to a common variable. When a function call occurs, the value of the array element used as the real parameter is passed to the form parameter to implement one-way value transfer.
B. Use the array name as the form parameter and real parameter of the function.


6. Definition of one-dimensional arrays and precautions
1) Introduction to one-dimensional arrays:
If all elements of an array are of the basic data type, the array is called a one-dimensional array.
2) Definition of one-dimensional array
To use arrays in C, you must first define them. Definition of one-dimensional arrays:
Type specifier array name [constant expression]
A. type description: any basic data type or constructed data type.
B. array name: User-defined array identifier.
C. constant expression in square brackets: the number of data elements, also known as the length of an array.
For example: int a [10]; // defines the integer array a, which has 10 elements
Float B [10], c [20]; // defines the real-type array B, which has 10 elements, defines the real-type array c, and has 20 elements.

Note: a. The length of an array can be a constant or a constant expression.
B. array naming rules: strictly follow the naming rules of the identifier.
C. The array name cannot be the same as other variables.
D. The length of the array cannot be a variable (required by the C99 standard)
E. The length of the array can be defined using a macro (# define M 5 int a [M] --> at this time, M is a macro definition)
F. When defining arrays, you can also define common variables. (For example, int x, y, z [9];)
3) one-dimensional array initialization:
Array initialization value assignment is to assign an initial value to an array during array definition. Array Initialization is performed in the compilation phase. This will reduce the running time and improve efficiency.
There are two initialization methods:
A. initialize the definition at the same time.
For example:
Type specifier array name [constant expression] = {value 1, value 2, value 3... value n };
Instance: int a [3] = {, 2}; // The length is determined by the value in [].
Int B [] = {2, 5, 7, 6}; // The length is determined by the number of values in {}.
Int c [10] = {[3] = 23, [8] = 34}; // an array with a definition length of 10. The value of the element with a subscript of 3 is 23, array element with subscript 8 is 34
B. Define and initialize
Type specifier array name [constant expression];
Array name [0] = value;
Array name [1] = value;
Array name [2] = value;
Instance: int d [10];
D [0] = 1; // an array element with a subscript of 0 is assigned a value of 1, that is, the first element value of the array is 1;
D [1] = 2; // assign a value to 2 to the array element whose subscript is 1; that is, the second element value of the array is 2;
D [2] = 3; // assign a value to 3 for the array element whose subscript is 2; that is, the third element value of the array is 3;
...
D [9] = 100; // an array element with a subscript of 9 is assigned 100; that is, the tenth element value of the array is 100;
4) confusing problems in one-dimensional arrays:
If the array is not initialized, is there a value in the array? If yes, what is the value? If the array is initialized, there are no values for those elements that are not initialized. If so, what are the values?
A. If the array is not initialized, the elements in the array have values. The value is the garbage value.
B. If the array is initialized, the elements that are not initialized have values. If yes, the value is 0.
C. If the character array is partially initialized, the uninitialized part also has a value. The value is the default value corresponding to the ASCII code.

5) reference method of one-dimensional array
An array element is the basic unit of an array. An array element is also a variable, which is identified by an array name followed by a subscript. Subscript indicates the sequence number of the element in the array.
Special emphasis: if some array elements are initialized, the system will not assign 0 values to those elements that are not initialized.

6) storage of one-dimensional arrays
Storage Method:
A. The computer will allocate a continuous storage space to the array;
B. the array name indicates the first address of the array. From the first address location, the first, second,..., and NTH elements of the array are saved in sequence;
C. Each element occupies the same number of bytes (depending on the data type)
D, and the addresses between elements are continuous.

7) Address of the one-dimensional array
Address of the one-dimensional array:
A. The element addresses in the array are continuous;
B. the array name stores the first address of the array --> the first address of the array: the first address of the first element of the array (the address of the first byte of the first element)
C. Supplement: array name = & array name [0]; array name is a constant that stores the first address of the array
D. address of each element in the array

8) one-dimensional array Length Calculation Method
A. because the number of characters occupied by the array in memory depends on the data type and number of data stored in the array.
B. Total number of bytes used by the array in memory: sizeof (array name );
C. Method for Calculating the array length:
Array length = Total number of bytes occupied by the array/number of bytes occupied by the array element
For example: int a [] = {, 36 };
Int a_len = sizeof (a)/sizeof (int );


9) out-of-bounds of one-dimensional arrays
The array subscript is insecure because it does not belong to you. Data may be lost.

10) Application: Find the maximum value
// Use the for loop to obtain 10 numbers from the keyboard and store them in the array. Find the maximum value of 10
Void main (){
Int a [10];
// Receives user input cyclically
For (int I = 0; I <10; I ++ ){
Printf ("Enter the number of % d \ n", I + 1 );
// Receives data and saves it to the array.
Scanf ("% d", & a [I]);
}
// Traverse the array and output the input 10 values.
For (int I = 0; I <10; I ++ ){
Printf ("% d \ t", a [I]);
}

// Find the maximum value
Int max = a [0];
For (int I = 0; I <10; I ++ ){
If (max = a [I];
}
}
Printf ("maximum input value: % d \ n", max );
}


11) array name as function parameter
A. Using an array element as a function parameter does not require the form parameter or an array element, but when using an array name as a function parameter, the parameters and the corresponding real parameters must be arrays of the same type.
B. in C language, in addition to the variable identifier, the array name also represents the starting address of the array in memory. Therefore, when the array name is used as a function parameter, the real-participation parameter is not a "value transfer", but an "Address Transfer". The real-participation parameter array will pass the starting address of the array to the parameter array. The two arrays share a memory unit, the compilation system no longer allocates storage units for the form parameter array.
C. When a variable is used as a function parameter, the value passing is unidirectional. That is, only real parameters can be transmitted to the form parameter, and real parameters cannot be returned from the form parameter. The initial values of the parameters are the same as those of the actual parameters. After the values of the parameters change, the real parameters do not change. The final values of the two parameters are different.

12) Notes on using array names as function parameters
A. the type of the parameter array and the real parameter group must be the same; otherwise, an error will occur;
B. The length of the parameter array and the actual parameter group can be different, because during the call, only the first address is transmitted without checking the length of the parameter array. When the length of the parameter array is different from that of the real parameter array, the program execution result is inconsistent with the actual result although the syntax error does not occur (compilation is successful.
C. In the function parameter table, the length of the parameter is not allowed, or a variable is used to represent the number of array elements. For example, you can write as follows:
Void nzp (int a []) or enter void nzp (int a [], int n)
The row parameter group a does not provide the length, but the n value dynamically represents the length of the array. The value of n is passed by the real parameter of the main function.
D. Multi-dimensional arrays can also be used as function parameters. When defining a function, you can specify the length of each dimension for the row parameter group, or you can save the length of the first dimension. Therefore, the following statements are legal:
Int MA (int a [3] [10]) or: int MA (int a [] [10])

13) Notes on using array names as function parameters
A. C language specifies that no matter what type of data, the memory address of the Data occupies 8 bytes in the memory.


14) Bubble Sorting
Bubble sort is a simple sorting algorithm.
Bubble Sorting is divided:
A. Sinking of large numbers
B. Floating Point
Bubble sorting steps:
A. Compare adjacent elements. If the first one is bigger than the second one, they will switch their positions.
B. perform the same operation on each adjacent element, from the first to the last. At this point, the final element should be the largest number.
C. Repeat the preceding steps for all elements except the last one.
D. Continue to repeat the above steps for fewer and fewer elements until no pair of arrays needs to be compared.
Code implementation:
Void maoPao (int nums [], int len ){
Int temp;
For (int I = 0; I For (int j = 0; j If (nums [j] Temp = nums [j];
Nums [j] = nums [j + 1];
Nums [j + 1] = temp;
}
}
}
}

Void main (){
Int nums [10] = };
Printf ("Before sorting :");
For (int I = 0; I <10; I ++ ){
Printf ("% d \ t", nums [I]);
}
MaoPao (nums, 10 );
Printf ("sorted :");
For (int I = 0; I <10; I ++ ){
Printf ("% d \ t", nums [I]);
}
}



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.