1. One-dimensional arrays
Arrays are constructed data types
Distinguish between array elements and arrays (int a[10] a[10])
The subscript must be guaranteed not to cross the border
An array is a whole and cannot be directly involved in an operation, only a single element can be processed.
2. Array sort (bubble sort)
int a[7] = {3, 1, 7, 23, 12, 87, 2};
The outer loop controls the number of trips to compare, and n elements need to compare n-1 times
for (int i = 0; i < 6; i++) {
Inner loop control in each trip to compare the number of times, each come in one less comparison
for (int j = 0; J < 6-i; J + +) {
If the previous element is larger than the latter, the swap position
if (A[j] < A[j + 1]) {
int temp = A[j];
A[J] = a[j + 1];
A[j + 1] = temp;
}
}
}
Print a sorted array
for (int i = 0; i < 7; i++) {
printf ("%d", a[i]);
}
Select sort
int a[5] = {7, 4, 6, 2, 1};
int k = 0;
for (int i = 0; i < 4; i++) {
K = i;//assumes that the a[i] element is the minimum value
for (int j = i + 1; j < 5; J + +) {
if (A[k] > A[j]) {
K = J;
}
}
if (i! = k) {
int temp = A[i];
A[i] = a[k];
A[K] = temp;
}
}
for (int i = 0; i < 5; i++) {
printf ("%-3d", A[i]);
}
3, character array
Char c[] = {' I ', ' P ', ' h ', ' o ', ' n ', ' e '};//length of 6
C language string ends with a
Char a[6] = "iPhone";//Length of 7 ' + '
unsigned long result = strlen (a);//The calculation is not required to meet the end of the
unsigned long s = sizeof (a);//sizeof () Memory size
printf ("%s", a);
The space occupied by the string is at least 1 larger than the string length, because the string ends with '/'. The string handler provided by the system determines whether the string ends according to '% '.
printf ("%ld", result);
printf ("%ld", s);
Strlen () Calculate string? length
strcpy () string copy shell
strcat () string concatenation
strcmp () string? comparison
One-dimensional arrays, array sorting, character arrays