C language programming uses array to process batch data

1 Why use arrays

A group of data with the same attribute is called an array . The C language stipulates that the number in square brackets is used to represent the subscript, such as s[15] for s15. In this way, several data with the same attribute are organized into a whole.
Data is a collection of ordered data.
In C language programs, arrays are often defined as needed, and loops are used to manipulate the elements in the array.

2 How to define and reference a one-dimensional array

2.1 How to define a one-dimensional array

When defining the array, you need to specify the type of this batch of variables, the name of the array, and how many elements (that is, variables) contained in the array. For example:
int a[10];
It means that an integer array is defined, the array name is a, and the array has 10 elements.
The way to define a one-dimensional array is:
type symbol array name [constant expression];
instructions for use:
(1) The naming rule of the array name is the same as that of the variable name, and it follows the identifier naming rule.
(2) The constant expression in square brackets is used to indicate the number of elements, that is, the length of the array. The subscripts start from 0.
(3) Constant expressions can include constants and symbol variables, but cannot include variables . For example:
Insert picture description herethis is wrong.

2.2 How to refer to the elements of a one-dimensional array

The array must be defined before the elements in the array can be referenced. Only one reference array element can be used and not all elements in the entire array can be referenced at once. For example:
t=a[2]; correct
printf("%d %d %d\n",a); wrong

The representation form of referencing array elements :
array name [subscript]
such as: a[5] represents the element with sequence number 5 in the a array.

Subscripts can be integer constants or integer expressions. For example:
a[2+3], a[2*3], a[7/3] are equivalent to a[5], a[6], a[2].

Example:
Quoting array elements. Use the loop structure to assign the values ​​0-9 to the array a[0] ~a[9], and then output the value of each element in reverse order.
Programming:
Insert picture description here
Program analysis: The
first for loop assigns the values ​​0~9 to the array a[0] ~a[9]. When the loop is executed for the first time, i=0, so a[0]=0, And so on.
The second for loop outputs the 10 elements in the a array in reverse order. The initial value of i is 9, so a[9] is output first, and then a[8] is output until a[0].

2.3 Initialization of one-dimensional array

(1) Assign initial values ​​to all array elements when defining the array. For example:
Insert picture description here
The initial values of the array elements are sequentially placed inside a pair of braces, according to the order assigned to the corresponding array element. That is, a[0]=0, a[1]=1, and a[2]=2.
(2) You can assign values ​​to only part of the elements. For example: the
Insert picture description here
defined array a has 5 elements, the first 3 elements are assigned, and the initial value of the last 5 elements is automatically 0.
(3) When defining the array, do not specify the length of the array. The system will determine the length of the array according to the sequence of data. For example: it
Insert picture description here
can be written as
Insert picture description here
there are 5 numbers in the braces, and the system will automatically define the length of the a array as 5 based on this.

2.4 Example of one-dimensional array program

Use arrays to solve the problem of finding Fibonacci numbers. Output the first 20 numbers in the sequence.
Idea: The first number in the array is placed in the first element of the array, and the second number in the array is placed in the second element of the
array ... The value of the element with the sequence number i of the array is its first two elements The sum of values.
Writing program:
Insert picture description here
bubble sorting
General form: sort n numbers in ascending order.
Principle: Big numbers sink, small numbers rise.
If there are n numbers, then n-1 rounds of comparison are required. In the first round of comparison, n-1 pairwise comparisons are performed, and in the j-th round of comparison, nj pairwise comparisons are performed.
After each round of comparison and exchange, the smallest number rises by one, and finally rises to the first number.
Example: Sort 9 8 5 4 2 0 in ascending order.
analysis:
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Sorting result:
0 2 4 5 9
Writing program:
Insert picture description here
Program analysis:

3 How to define and reference a two-dimensional array

3.1 How to define a two-dimensional array

Define a two-dimensional array, for example;
Insert picture description here
define a as a 3×4 (3 rows and 4 columns) array, and b as a 5×10 (5 rows and 10 columns) array.
Define the general form of a two-dimensional array:
type name array name [constant expression] [constant expression];
the order of the elements in a two-dimensional array is stored in rows, and the elements of the first row are stored in the memory first, and then stored Element of the second row. The storage order of the a[3][4] array is as follows: (logical concept)
Insert picture description here
The conceptual diagram on the physical layer is as follows:

Insert picture description here

3.2 How to refer to the elements of a two-dimensional array

The representation of two-dimensional array elements is:
array name [subscript][subscript]
subscript can be an integer constant or an integer expression, such as [2*1-1][1+1]
subscript The value cannot exceed the range of the array size.

3.3 Initialization of two-dimensional array

(1) Assign initial values ​​to the two-dimensional array in a branch. For example:
Insert picture description here
the data in the first brace is assigned to the element in the first line, the data in the second brace is assigned to the element in the second line...the initial value is assigned by line.
Insert picture description here
(2) All data can be written in a curly bracket, and initial values ​​are assigned to each element in the order of array arrangement. For example:
Insert picture description here
write a large piece, it is easy to miss, not easy to check.
(3) Assign initial values ​​to some elements. For example:
Insert picture description here
Insert picture description here
(4) Assign the initial value and the length of the second dimension to all elements, and the system automatically calculates the length of the first dimension. E.g:
Insert picture description here

In the definition, you can also assign initial values ​​to only part of the elements and omit the length of the first dimension, but the initial values ​​should be assigned in separate lines. For example:
Insert picture description here
this way of writing can inform the system that the array has 3 rows, and the elements of the array are:
Insert picture description here

3.4 Example of a two-dimensional array program

The elements of a two-dimensional array of a swap of rows and columns (i.e., column transposed), to deposit another two-dimensional array b, for example:
Insert picture description here
solving ideas:
the array in a row i and column j is assigned to the element b The elements in row j and column i in the array, for example, a[0][0] is assigned to b[0][0], a[0][1] is assigned to b[1][0]...
Insert picture description here
Use a double loop to process, The outer loop controls the change of rows, and the inner loop controls the change of columns.
Insert picture description here

4 character array

The array used to store character data is a character array . An element in the character array stores a character.

4.1 How to define and initialize character array

To store 4 characters of "ab c" (including spaces) in an array, you can do this:
Insert picture description herefirst define the array c, and then use the assignment statement to assign the characters to the elements in the character array.

You can also assign an initial value to each element when defining a character array, that is, initialize it. For example:
Insert picture description here
assign 4 characters to the 4 elements c[0]~c[3].
Insert picture description here
If the number of initial values ​​is less than the length of the array, these characters are assigned to the previous elements in the array, and the elements of the area are automatically set as null characters (ie,'\0'). For example: the
Insert picture description here
array status is as follows:
Insert picture description here

The system will automatically determine the length of the array based on the number of initial values. For example:
Insert picture description here
the length of the array c is automatically set to 10. This method can save the calculation of the number of arrays, which is more convenient.

4.2 How to quote character arrays

You can quote an element in the character array to get a character.
Example: Output a diamond chart.
Writing a program:
Insert picture description here
Analysis: The
number of rows is not specified when defining the character array d, because 5 braces have been used in the initial value assignment, indicating that it is assigned to the elements in the 5 rows, so there is no need to specify the number of rows when defining the number of characters , The system will automatically define the word array as 5 rows and 5 columns.

4.3 Strings and end-of-string markers

The system will automatically add a \0 as the terminator when processing string constants, and when it encounters the character \0, it means the end of the string. The program often relies on detecting the position of \0 to determine whether the string ends.
\0 represents a character with an ASCII code of 0, and a character with an ASCII code of 0 is a "null operator", that is, it does nothing but a distinguishing sign.
For example:
output a character string.
Insert picture description here
The system automatically adds a \0 after the last character \n as a string mark, and stops output when it encounters \0.

Use character constants to initialize character arrays. For example:
Insert picture description here
equivalent to
Insert picture description here

4.4 How to input and output character arrays

There are two methods for input and output.
(1) Input and output character by character. Use the format declaration %c to input or output a character.
(2) Input or output the entire string at once. Use the format declaration %s, which means the input and output of the string. For example:
Insert picture description here
the state of the array c in the memory is as follows:
Insert picture description here

Instructions for use:
(1) The output characters do not include the terminator \0.
(2) When outputting a character string with the %s format character, the output item in the printf function is the name of the character array, not the name of the array element.
(3) If the length of the array is greater than the actual length of the string, it will only output until it ends with \0. For example:
Insert picture description here
only output the valid character "abc" of the string, instead of outputting 10 characters, this is the advantage of using the end of the string to mark.
(4) If there is more than one \0 in the character array, the output will end when the first \0 is encountered.
(5) You can use the scanf function to input a character string. For example: the
Insert picture description here
input item c is the name of the defined character array, and the input string should be shorter than the length of the defined character array.
If you use a scanf function to input multiple strings , they are separated by spaces when inputting . For example:
Insert picture description here
Input data:
Are you ok?
After input, the array status of str1, str2, and str3 is as shown in the figure below.
Insert picture description here
(6) If the input item in the scanf function is a character array name, do not add the address character &, because the array name in the C language represents the starting address of the array . The following is wrong:
Insert picture description here
(7) If you want to know the starting address of the array str in the memory, you can use the following output statement:
Insert picture description here
the starting address of the array str in decimal form.
(8) If the following output statement:
Insert picture description here
Find the starting address of the str array according to the array character array name str, and then output the characters in it one by one until \0 is encountered.

4.5 String processing functions

Insert picture description here

4.6 Examples of character array applications

There are 3 strings, "China", "Japan", and "India". It is required to find the "largest" among them.
The "larger" character refers to the character with the larger ASCII code. For example: the character'B' is greater than the character'A', and the character'a' is greater than the character'A'. If it is a character string, start with the first character and compare them one by one. If the first character is the same, compare the next character until it is different. If the strings are all in English, they are sorted according to the alphabet, and the next position in the alphabet is the largest, for example, "then"<"they".
Idea:
Define a two-dimensional character array with a size of 3×10, 3 rows and 10 columns, and each column can hold 10 characters (including the final terminator \0). The situation of a two-dimensional array is as follows:
Insert picture description here
Writing a program:
Insert picture description here
Analysis:
When using a string function, use #include <string.h> at the beginning of the program.
The space character participates in the comparison, and it is "greater than" any alphabetic character.

5 Improved part

5.1 Why are two pairs of double parentheses used when defining a two-dimensional array?

Define a two-dimensional array, for example;
Insert picture description here
define a as a 3×4 (3 rows and 4 columns) array, and b as a 5×10 (5 rows and 10 columns) array.
Benefit:
Yes, the two-dimensional array can be regarded as a special one-dimensional array, a includes 3 elements, and each element is a one-dimensional array containing 4 elements. The two-dimensional number defined above can be understood as the definition of three one-dimensional arrays, which is equivalent to:
Insert picture description here
a[0], a[1], a[2] as one-dimensional array names. Indicates that the one-dimensional array a[0] contains 4 elements.

5.2 A detailed description of C's string functions

5.2.1 gets function (read string function)

The function is to input a string from the terminal into a character array. Execution function: gets(str)
Input from the keyboard: abc
sends the input string "abc" to the character array str (note that there are a total of 4 characters sent to the array).

5.2.2 puts function (output string function)

The general form of the puts function is: puts (character array);
its function is to output a string (character sequence ending with'\0') to the terminal.
Insert picture description here
The string output by the puts function can contain escape characters. E.g:
Insert picture description here

5.2.3 strcat function (string concatenation function)

The general form of the strcat function is:
strcat (character array 1, character array 2)
strcat is the abbreviation of STRing CATenate (string concatenation). Its function is to connect the strings in the two character arrays, connect the string 2 to the back of the string 1, and put the result in the character array 1. For example:
Insert picture description here
Explanation:
(1) The length of character array 1 must be large enough to accommodate the new string after concatenation.
(2) There is a'\0' after the two strings before the connection, the'\0' after the string 1 is canceled during the connection, and only the'\0' is reserved in the string after the connection.

5.2.4 strcpy function (string copy function)

The general form of the strcpy function:
strcpy (string array 1, string 2)
strcpy is shorthand for STRing CoPY (string copy). The function is to copy the string 2 to the character array 1 . For example:
Insert picture description here
after execution, the status of c is as follows:
Insert picture description here
Explanation:
(1) The length of character array 1 should not be less than the length of string 2.
(2) Character array 1 must be written in the form of an array name (such as c1), and string 2 can be a character array name or a string constant . For example:
Insert picture description here
(3) You cannot use an assignment statement to directly assign a character constant or character array to a character array. Such as: it
Insert picture description hereis wrong.
(4) You can use the strncpy function to copy the first n characters in the string 2 to the character array 1. For example: the
Insert picture description here
function is to copy the first two characters in str2 to str1, expecting the original first two characters in str1. But the number of copied characters n should not be more than the original characters in str1 (excluding \0).

5.2.5 strcmp function (string comparison function)

The general form of the strcmp function is:
strcmp (string 1, string 2)
strcmp is the abbreviation of STRing CoMPare (character comparison). Its role is to compare string 1 and string 2. For example:

string comparison rules: compare two strings from left to right by chasing characters (compare according to the ASCII code value), and guide until different characters appear or until they encounter'\0'. If all characters are the same, they are considered equal; if different characters appear, the comparison result of the first different character shall prevail.
If the two strings to be compared are composed of English letters, the one with the position behind in the English dictionary is "big". For example: "then"<"they".
But it should be noted that lowercase letters are "bigger" than uppercase letters.
The result of the comparison is brought back by the function value.
(1) If string 1=string 2, the function value is 0.
(2) If string 1>string 2, the function value is a positive integer.
(3) If string 1 <string 2, the function value is a negative integer.
To compare two strings, only the strcmp function can be used.

5.2.6 strlen function (function for measuring string length)

The general form of the strlen function:
strlen (character array)
strlen is the abbreviation of STRing LENgth (string length), the function is to measure the length of the string, the function value is the actual length in the string (not including'\0') ). For example: the
Insert picture description here
output is 3, not 8, nor 4.

5.2.7 strlwr function (converted to lowercase character function)

The general form of the strlwr function:
strlwr (string)
strlwr is the abbreviation of STRing LoWeRcase (lowercase string). The effect is to replace uppercase letters in the string with lowercase letters.
Insert picture description here

5.2.8 strupr function (function to convert uppercase characters)

The general form of the strupr function:
strupr (string)
strupr is the abbreviation of STRing UPpeRcase (string uppercase). The function is to replace the lowercase letters in the string with uppercase letters.
Insert picture description here

6 Summary

An array is a collection of ordered data.
When defining an array, pay special attention to the problem of "subscript out of bounds".
Distinguish between the definition of array and the reference form of array elements.
The arrangement order of the elements of the two-dimensional array is "arranged in rows". When initializing a two-dimensional array, assign initial values ​​to rows.
Strings are stored in the form of character arrays.
\0 is not a part of the string, it is the end of the string.
The operation on the character string should be carried out through the character string function. When using string functions, #include<string.h> must be used at the beginning of this program.
The name of the array represents the address of the first element of the array , not the value of all the elements in the array.

Guess you like

Origin blog.csdn.net/qq_45059457/article/details/113926813