One-dimensional arrays
Data Type Array name [] = { };
In the standard C language environment, the array size can only be used in constants
int array[1000]={}; for (int i =0; i <; i++) { printf ("%d", Array[i]); } Print out a random number, but most of it is 0.
The first type: Direct initialization assignment:
int array[10]={1,2,3,4}; for (int i = 0; i < ten; I + +) { printf ("%d", Array[i]); }
Run Result: (0 is not defined later until the length of the array is reached)
1 2 3 4 0 0 0 0 0 0
Assign values directly to all values 0
int array[10]={0};
The second type: first declaration after the assignment value:
int array[10]; Value: array name [subscript] Subscript: 0-(n-1) //input number, fill the array for (int i = 0; i < x, i + +) { printf ("%d:", i+1); scanf ("%d", &array[i]); } Print array for (int i = 0; i <; i + +) { printf ("%d", Array[i]); }
Run Result: (need to enter manually, I have entered here.) )
Section 1 A: 2
Section 2 A: 1
Section 3 A: 1
Section 4 A: 1
Section 5 Total:23
Section 6 Total:123
1 No. 7 :2321
Section 8 Total:21
Section 9 Total:12
Section Ten Total:12
2 1 1 1 23 123 12321 21 12 12
Program ended with exit code:0
Example 1: Finding the largest value in an array
int array[10]={5,20,15,23,10}; int max=array[0]; for (int i =0; i < 5; i + +) { if (Max<array[i]) { max=array[i]; } } printf ("%d", max);
Operation Result:
23
Program ended with exit code:0
Example 2: initialize an array with an element of 60 85.5 90 56 100 67, Sum and average
Because there are decimals, float is used, where sizeof is my access to the size of the array, with no effect on the result
Float array[]={60,85.5,90,56,100,67}; float Sum=0,pi; for (int i = 0; i < 6; i + +) { sum+=array[i]; } PI=SUM/6; printf ("%lu\n", sizeof (double)); printf ("%lu\n", sizeof (float)); printf ("%lu\n", sizeof (array)); printf ("sum=%.2f,pi=%.2f", SUM,PI);
Operation Result:
8
4
24
sum=458.50,pi=76.42
Program ended with exit code:0
Sorting of arrays
Bubble sort, simplest sort
int array[5]={1,10,9,16,2}; Ideas: The next two books compared, from small to large, smaller rows to the front of the adjacent, round a round of comparison, less data can be used this method //First time: 1 9 10 2 16; Comparison: 4 //second: 1 9 2 10 16; Comparison: 3 //Third time: 1 2 9 10 16; Comparison: 2 //Third: 1 2 9 10 16; Comparison: 1 for (int i = 0; I < 5; i + +) { for (int j = 0; J < 5-i-1; J + +) { if (Array[j] > Array[j+1]) { //to use J instead of I (adjacent two number interchange) int temp = Array[j]; ARRAY[J] = array[j+1]; ARRAY[J+1] = temp;}} } for (int i = 0; i < 5; i + +) { printf ("%d", Array[i]); }
Operation Result:
1 2 9) 10 16
Program ended with exit code:0
Easy version Selection sorting
for (int i = 0; i< 5; i + +) {for (int j = i; j< 5; j + +) { if (Array[i] > Array[j] ) { int temp = Array[i]; Array[i] = array[j]; ARRAY[J] = temp; } } printf ("%d", Array[i]); }
Operation Result:
1 2 9) 10 16
Program ended with exit code:0
two-dimensional arrays
A two-dimensional array is generally defined like this:
<pre name= "code" class= "OBJC" > int sz[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
Of these, 3 and 4 are can be changed, 3 is how many lines, 4 are there on the less column, written so may be better understood:
int Sz[3][4] = {{1, 2, 3, 4},
<span style= "White-space:pre" ></span> {5, 6, 7, 8},
<span style= "White-space:pre" ></span> {9,10,11,12}};
Here's a look at it by example.
Also find the maximum value in the array
int array[][4] ={1,2,3,4,5,6,7,8,9};// //The best assignment to Max is the value in the array to avoid error int max=array[0][0]; for (int i = 0; i < 3; i + +) {for (int j = 0; J < 4; J + +) { if (Max < array[i][j]) { max = array[i][ j];}} } printf ("max=%d", Max);
Operation Result:
Max=9
Program ended with exit code:0
Above is a way, we now use a loop to print the values in the array, and get the largest number
int array[][4] ={1,2,3,4,5,6,7,8,9};// //The best assignment to Max is the value in the array to avoid error int max=array[0][0]; A two-dimensional array with a one-dimensional array to print out, direct two-dimensional into a one-dimensional way to print the line, a row of all the data for (int i =0; i<12;i++) { //become a one -dimensional dozen printf ("%d ", Array[0][i]); if (Max<array[0][i]) { max=array[0][i]; } printf ("%d", array[0][0]); } printf ("\ n"); printf ("max=%d", Max);
Operation Result:
1 2 3 4 5 6 7 8 9 0 0 0
Max=9
Program ended with exit code:0
Character array
A character array is a string
It can be defined in several ways:
Char string[] = "Hello"; Char string1[6] = {' h ', ' e ', ' l ', ' l ', ' o '};
Let's take a look at the following example: (compare whether the string is the same or whether the characters in it are the same)
As for the string1 inside why is 6, because there is a string after the ' s ', to occupy a character, if less than 6 words in front of the same output, the following will appear garbled
Char string1[6] = {' h ', ' e ', ' l ', ' l ', ' o '}; Char string3[] = "Wrold"; Max cannot accept string char max; for (int i = 0;i < 5; i + +) { if (String1[i]>string3[i]) { printf ("large"); } else if (String1[i]<string3[i]) { printf ("small"); } else{ printf ("Medium"); } }
Operation Result:
Small and medium size
Program ended with exit code:0
There are also special strings in the C language of the Operation method
strcmp (< #const char *#>, < #const char *#>) Compare size //strcpy (< #char *#>, < #const char *#>) copy
//strlen (< #const char *#>) Gets the string length //strcat (< #char *#>, < #const char *#>) Link
There's no example here.
Arrays in the C language