Dark Horse Programmer--ios Learning notes--arrays and sorting

Source: Internet
Author: User
Tags modifiers

iOS Learning Notes

Overview:

8_2, changing the symbol of an integer variable
8_2, changing the storage space of integer variables
8_3, char type data storage
Basic concepts and classification of 8_4 and arrays
8_5, array elements as function arguments
8_5, one-dimensional array definitions and considerations
8_6, one-dimensional array initialization
8_7, one-dimensional arrays a confusing question
8_8, one-dimensional array reference
8_9, application: Array traversal
How to store 8_10 and one-dimensional arrays
8_11, one-dimensional array address
8_12, one-dimensional array length calculation method
Cross-border problem of 8_13 and one-dimensional arrays
8_14, Application: Find maximum Value
8_15, array elements as function arguments
8_16, array name as function parameter
8_17, array names as note points for function parameters
8_17, array names as note points for function parameters-Supplemental
The thought of 8_18 and bubbling sort
8_18, bubbling sort of thought--supplementing
8_19, bubble Sort Code implementation


Introduction to shaping variable modifiers (modifiers include: int short long "long Long" signed unsigned)


1. Change the storage space of shaping variables
Short 2 byte output with%HD short int a=4;--> occupies two bytes of printf ("%hd\n", variable name);
Example: Short int a=1;
Short int a1=a<<15; Move A to the left 15 bits, the smallest negative number
A1= (a<<15)-1; Move left 15-bit, maximum positive number
printf ("d%\n", A1);
Short modified int accounted for two bytes, total 16 bits
The scope of the representation is:
10000000 00000000--the smallest negative number-32768
01111111 11111111--Maximum positive 32767


int 4 bytes Output with%d or%i---Four bytes
For example: int b=1;
int b1=b<<31;
B1= (b<<31)-1; Move left 31-bit
printf ("d%\n", B1);
The scope of the representation is:
10000000 00000000 00000000 00000000--the smallest negative number-2147483648
01111111 11111111 11111111 11111111--Maximum positive 2147483647


A long 8 byte output with%LD --occupies eight bytes
For example: Long int c=1;
Long int c1=c<<63; Move left 63-bit
C1= (c<<63)-1;
printf ("C1 = ld%\n", C1);
The scope of the representation is:
10000000 00000000 00000000 00000000 ... 00000000 (64 bits)--minimum negative number-9223372036854775808
01111111 11111111 11111111 11111111 ... 11111111 (64)--Maximum positive 9223372036854775807


Long long 8 bytes output with lld% (mainly to solve 32-bit machine problems)--eight bytes in total
Note: All modifiers are used to modify the shaping variable.
* Long Long type
Under 32-bit systems:
Long takes 4 bytes (changes the storage space of the shaping variable)
Long long takes 8 bytes
Under 64-bit systems:
Long takes 8 bytes
Long long takes 8 bytes


2. Change the symbol of the shaping variable
Signed representation is signed (computer is signed by default)
Unsigned is an unsigned number (changing the symbol of the shaping variable, and the range of positive values is enlarged by one-fold.) )Output with u% printf ("u%\n", variable name);






3. Char type data storage
1) storage problem for Char type constants
sizeof (' a ')
--First find the ASCII code value of ' a '-->97
--97 according to the INT type-->00000000 00000000 00000000 01100001
Char ch= ' a '
-->97
--Convert 97 to 2 binary 11,000,011 bytes
--Storing one byte in memory
Summary: Constants and variables of type char are stored differently. (Note: In the Xcode environment.) If it may be the same in other environments)
A byte range: 0--127 used as the ASCII code for common characters




4. Basic concepts and classification of arrays
1) Basic Concepts:
In the program design, for the convenience of data processing, we have a number of variables of the same type organized in an orderly manner, these ordered array of similar data elements are called arrays.
In the C language, an array is a constructed data type. An array can be decomposed into multiple array elements, which can be primitive types or constructed types.
2) Several nouns of an array:
What is an array: An ordered set of data that has the same data type.
Array elements: The data that makes up the array. Each element in the array has the same name, a different subscript, and can be used as a single variable, so it is also called a subscript variable.
Subscript of an array: an index or an indication of the position of the array element.
The number of dimensions of the array: is the number of array subscripts. The dimensions of the data array can be divided into one-dimensional arrays, two-dimensional arrays, three-dimensional arrays, and multidimensional arrays.


Categorized by stored content: Different types of array elements, arrays are divided into:
Array of values: used to store arrays
Character array: used to store characters
Pointer array: Used to hold pointers (addresses)
Array of structures: data used to hold a struct type
Classification by Dimension:
One-dimensional arrays:
Two-dimensional arrays:
Three-dimensional arrays:
Multidimensional Arrays:


5. Array elements as function parameters
1) The array can be used as a function parameter for data transfer.
A, one is to use the array element (subscript variable) as an argument;
The array element is the subscript variable, and it is not different from the normal variable. So it is exactly the same as the normal variable when the function argument is used, and when the function call occurs, the value of the array element that is the argument is passed to the formal parameter, and the one-way value is passed.
b, one is to use the array name as a function of the formal parameters and arguments.


6. Definition of one-dimensional array and considerations
1) Introduction of one-dimensional arrays:
If all elements of an array are basic data types, then the array is called a one-dimensional array
2) Definition of one-dimensional array
The use of arrays in the C language must be defined first. How one-dimensional arrays are defined:
Type descriptor array name [constant expression]
A, type specifier: is either a basic data type or a constructed data type.
B, array name: User-defined array identifier.
C, constant expression in square brackets: Represents the number of data elements, also known as the length of the array.
Example: int a[10]; Defines the shape array A, with 10 elements
float B[10],C[20]; Defines a real array B, has 10 elements, defines a real array of C, and has 20 elements.

Note: A, the length of an array can be a constant, or it can be a constant expression.
b, the name of the array of rules: strictly in accordance with the name of the symbol specification.
C, the array name cannot be the same as other variables.
D, the length of the array cannot be considered as a variable (required by the C99 standard)
E, the length of the array can be defined with a macro (#define M 5 int a[m]-and at this point m is a macro definition)
F, when defining an array, you can also define normal variables. (eg: int x,y,z[9];)
3) Initialization of one-dimensional arrays:
Array initialization assignment refers to assigning an initial value to an array when the array is defined. Array initialization is done during the compile phase. This will reduce running time and increase efficiency.
There are two ways of initializing:
A, the definition of simultaneous initialization
For example:
Type specifier array name [constant expression]={value 1, value 2, value 3 ... Value n};
Example: int a[3]={1,4,2}; Length is determined by the value in []
int b[]={2,5,7,6}; Length is determined by the number of values in the {}
int c[10]={[3]=23,[8]=34};//defines an array of length 10, the value of the element labeled 3 is 23, and the array element labeled 8 is 34.
b, define first, then initialize
Type specifier array name [constant expression];
Array name [0]= value;
Array name [1]= value;
Array name [2]= value;
Example: int d[10];
The d[0]=1;//is assigned a value of 1 for the array element with the subscript 0, i.e. the first element of the array has a value of 1;
The d[1]=2;//is assigned a value of 2 for the array element with the subscript 1, i.e. the second element of the array has a value of 2;
The d[2]=3;//is assigned a value of 3 for the array element with the subscript 2, i.e. the third element of the array has a value of 3;
...
The d[9]=100;//is assigned a value of 100 for the array element with the subscript 9, that is, the tenth element of the array has a value of 100;
4) One-dimensional array of confusing questions:
If the array is not initialized, is there a value in the array? If so, what is the value? If the array part is initialized, then those elements that are not initialized have no value, and if so, what is the value?
A, if the array is not initialized, the elements in the array are valued. Value is a garbage value.
b, if the array part is initialized, then those elements that are not initialized have values, if any, the value is 0.
C, if the character array is partially initialized, then the part that is not initialized has a value, and the value is the default value of the ASCII code.

5) How one-dimensional arrays are referenced
An array element is the basic unit that makes up an array. An array element is also a variable that is identified by an array name followed by a subscript. The subscript indicates the ordinal number of the element in the array.
In particular, the system does not assign a value of 0 to a part of an element that is not initialized if some array elements are initialized by using the first defined post-initialization method.

6) How one-dimensional arrays are stored
Storage mode:
A, the computer will assign a contiguous storage space to the array;
b, the array name represents the first address of the array, from the first address location, sequentially into the array of the first, second 、...、 nth element;
c, each element occupies the same number of bytes (depending on the data type)
D, and the address between the elements is continuous.

7) Address of one-dimensional array
Address of one-dimensional array:
A, the element address inside the array is continuous;
b, the array name is the first address of the array---the first address of the array: first address of the first element of the array.
C, Supplementary: array name ==& array name [0]; The array name is a constant that stores the first address of the array.
D, the address of each element of the array

8) One-dimensional array length calculation method
A, because the number of characters the array occupies in memory depends on the type of data it stores and the number of data
b, the total number of bytes that the array occupies in memory: sizeof (array name);
C, the method of calculating the length of the array:
The length of the array = The total number of bytes consumed by the array/number of bytes occupied by the array element
For example: int a[]={1,5,3,2,7,4,9,23,46,47,36};
int a_len=sizeof (a)/sizeof (int);


9) Cross-border problem of one-dimensional arrays
Array subscript out of bounds is unsafe because this space does not belong to you. There may be data loss occurring.

10) Application: Find maximum Value
With a For loop, get 10 numbers from the keyboard and store them in the array. Find the maximum value in 10 numbers
void Main () {
int a[10];
Loop receive user input
for (int i=0;i<10;i++) {
printf ("Please enter number%d \ n", i+1);
Receive data, save to array
scanf ("%d", &a[i]]);
}
Iterate through the array, output to see the 10 values entered
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]) {
Max=a[i];
}
}
printf (The maximum value entered is:%d\n, max);
}


11) Array name as function parameter
A, using an array element as a function parameter does not require that the formal parameter must also be an array element, but when using the array name as a function parameter, both the formal parameter and the corresponding argument must be an array of the same type.
b, in the C language, the array name in addition to the identifier of the variable, the array name also represents the array in the memory of the starting address, so when the array name as a function parameter, the actual participating parameters are not "value pass", but "address delivery", the real parameter group name the array's starting address passed to the parameter group, Two arrays share a memory unit, and the compilation system no longer allocates a storage unit for the parameter group.
C, when a variable is a function parameter, the value passed is one-way. That is, you can only pass arguments to a parameter, and you cannot return an argument from a formal parameter. The initial value of the formal parameter is the same as the argument, and when the value of the formal parameter changes, the argument does not change, and the final values are different.

12) array name as a note point for function parameters
A, shape parameter group and real parameter group must have the same type, otherwise it will cause an error;
The length of the B, the parameter group, and the real parameter group can be different, because at the time of invocation, only the first address is transmitted without checking the length of the shape parameter group. When the length of the parameter group is inconsistent with the real parameter group, it should be noted that although the syntax error is not present (compilation can pass), the results of the program execution will not match the actual.
c, in the function parameter list, allows to not give the length of the formal parameters, or a variable to represent the number of array elements. For example, it can be written as:
void Nzp (int a[]) or written as: void Nzp (int a[],int N)
Where the row parameter group A does not give the length, and the N value dynamically represents the length of the array. The value of n is passed by the argument of the keynote function.
D, multidimensional arrays can also be used as arguments to functions. On a function definition, you can specify the length of each dimension for a row parameter group, or you can omit the length of the first dimension. Therefore, the following wording is legal:
int ma (int a[3][10]) or: int ma (int a[][10])

13) Array name as a note point for function parameters
A, c specifies that, regardless of the type of data, the memory address of the data occupies 8 bytes in memory


14) Bubble Sort idea
Bubble sort (Bubble sort) is a simple sort algorithm.
Bubble sorting is divided into:
A, large number sinking
B, fractional float
Bubble Sort Step:
A, compare adjacent elements. If the first one is bigger than the second one, swap their positions.
b, for each pair of adjacent elements do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number.
C, repeat the above steps for all elements, except for the last one.
D. Repeat the above steps each time for fewer elements until there are no pairs of arrays to compare.
Code implementation:
void Maopao (int nums[],int len) {
int temp;
for (int i=0;i<len-1;i++) {
for (int j=0;j<len-1-i;j++) {
if (Nums[j]<nums[j+1]) {
TEMP=NUMS[J];
NUMS[J]=NUMS[J+1];
Nums[j+1]=temp;
}
}
}
}

void Main () {
int nums[10]={1,5,2,7,6,9,3,4,15,74};
printf ("Before sorting:");
for (int i=0;i<10;i++) {
printf ("%d\t", Nums[i]);
}
Maopao (nums,10);
printf ("After sorting:");
for (int i=0;i<10;i++) {
printf ("%d\t", Nums[i]);
}
}



Dark Horse Programmer--ios Learning notes--arrays and sorting

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.