C language basics (5) definition and use of arrays


A first look at C language in series collection

6. Definition and use of arrays

6.1 Definition and use of one-dimensional array

6.1.1 Definition of one-dimensional array

Type specifier array name [constant expression]
type specifier: any basic data type or constructed data type
array name: user-defined array identifier
constant expression: the number of array elements
such as: int a[10] Description Integer array a with 10 elements

6.1.2 References to one-dimensional array elements

The general form of array elements is: Array name [array subscript]
The following table can be integer constants, variables, or integer expressions.
For example: x = a[i];
a[0] = a[5] + a[7] - a[2*3];

6.1.3 Initialization of one-dimensional arrays

The general form of initialization assignment is:
type specifier array name [constant expression] = {value 1, value 2, ..., value n};
regulations:
1) You can only assign initial values ​​to some elements
2) You can only assign elements one by one Assignment, you cannot assign values ​​to the entire
array 3) If you assign values ​​to all elements, the number of array elements may not be given in the array description
Some explanations for the definition of a one-dimensional array:
1) The type of the array actually refers to the number of array elements Value type
2) The writing rules of the array name should conform to the writing rules of the identifier
3) The array name cannot be the same as other variable names
4) The subscript of the array element starts from 0, and the maximum value is the size of the array minus 1
5) Cannot be in the square Variables are used to represent the number of elements in brackets [], but they can be symbolic constants or constant expressions
6) It is allowed to describe multiple arrays and multiple variables in the same type description

6.2 Definition and use of two-dimensional array

6.2.1 Definition of two-dimensional array

Type specifier array name [constant expression 1] [constant expression 2]
constant expression 1: the length of the first dimension subscript
constant expression 2: the length of the second dimension subscript
Arrangement order of two-dimensional array: store by row For
example :
a[0] ... a00 a01 a02 a03
a[1] ... a10 a11 a12 a13
a[2] ... a20 a21 a22 a23

6.2.2 References to two-dimensional array elements

Representation form:
array name [subscript] [subscript]
array elements can appear in the expression, and can also be assigned.
For example: b[1][2] = b[2][3]/2 The
subscript value should be in Within the range of the defined array size
For example: int a[3][4]; a[3][4] = 3; (beyond the range of the array size)

6.2.3 Initialization of two-dimensional array

1) Assign initial values ​​to two-dimensional arrays by row
For example: int a[3][4] = { {1,2,3,4} , {5,6,7,8} , {9,10,11,12 }}
2) All data can be written in curly braces, and initial values ​​are assigned to each element in the order of the array.
For example: int a[3][4] = {1,2,3,4,5,6, 7,8,9,10,11,12}
3) Initial values ​​can be assigned to some elements
4) Initial values ​​can be assigned to all elements, then when defining an array, the length of the first dimension can not be specified, but the length of the second dimension cannot be omitted

6.3 Example of array application

1. Use array processing to find the Fibonacci sequence problem

#include <stdio.h>
int main(){
    
    
	int i;
	int f[20] = {
    
    1,1};
	for(i = 2; i < 20; i++){
    
    
		f[i] = f[i - 2] + f[i - 1];
		printf("%12d", f[i]);
	}
}

2. Arrange 10 numbers with bubble sort

#include <stdio.h>
int main(){
    
    
	int a[10];
	int i, j, t;
	printf("please input 10 numbers : \n");
	for(i = 0; i < 10; i++){
    
    
		scanf("%d\n", &a[i]);
	}
	printf("\n");
	for(j = 1; j < 10; j++){
    
    
		for(i = 1; i <= 10 - j; i++){
    
    
			if(a[i] > a[i + 1]){
    
    
				t = a[i];
				a[i] = a[i + 1];
				a[i + 1] = t;
			}
		}
	}
	printf("the sorted number : \n");
	for(i = 0; i < 10; i++){
    
    
		printf("%d\n",a[i]);
	}
}

3. Exchange the row and column elements of a two-dimensional array into another two-dimensional array

#include <stdio.h>
int main(){
    
    
	int a[2][3] = {
    
    {
    
    1,2,3},{
    
    4,5,6}};
	int b[3][2], i, j;
	printf("array a : \n");
	for(i = 0; i < 2; i++){
    
    
		for(j = 0; j < 3; j++){
    
    
			printf("%5d", a[i][j]);
			b[j][i] = a[i][j];
		}
		printf("\n");
	}
	printf("array b:\n");
	for(i = 0; i < 3; i++){
    
    
		for(j = 0; j< 2; j++){
    
    
			printf("%5d",b[i][j]);
		}
		printf("\n");
	}
}

4. There is a 3*4 matrix to find the value of the element with the largest value, as well as its row number and column number

#include <stdio.h>
int main(){
    
    
	int i, j, row = 0, colum = 0, max;
	static int a[3][4] = {
    
    {
    
    1,2,3,4},{
    
    9,8,7,6},{
    
    -10,10,-5,2}};
	max = a[0][0];
	for(i = 1; i < 3; i++){
    
    
		for(j = 0; j < 4; j++){
    
    
			if(a[i][j] > max){
    
    
				max = a[i][j];
				row = i;
				colum = j;
			}
		}
	}
	printf("max = %d, row = %d, colum = %d\n", max,row, colum);
}

6.4 Character arrays

6.4.1 Definition of character array

type-specifier array-name[constant-expression]

6.4.2 Initialization of character arrays

1) Assign character by character to each element in the array
2) If the number of initial values ​​provided in curly braces is greater than the length of the array, the syntax is wrong; if the number of initial values ​​is less than the length of the array, only these characters are assigned to the first elements in the array , and the rest are "\0"
3) If the number of initial values ​​provided is the same as the predetermined array length, the array length can be omitted when defining, and the system will automatically determine the array length according to the number of initial values
​​4) Two-dimensional character arrays can be defined

6.4.3 End of string flag

Strings are processed with character arrays, end identifier "\0"

6.4.4 Input and output of character arrays

1) Input and output character by character, use the format character %c to input or output a character.
2) To input and output the entire string at one time, use the %s format character.
3) If the length of the array is greater than the actual length of the string, only the output will end when "\0" is encountered.
4) If a character array contains more than one "\0", the output will end when the first "\0" is encountered.
5) You can use the scanf function to input a string.

6.5 String processing functions

6.5.1 puts

puts (array of characters):
Write a string to stdout up to but not including a null character. Newline characters are appended to the output.

char str[] = {
    
    "China--Beijing"};
puts(str);
//运行结果为:China--Beijing

6.5.2 gets

gets (character array):
Read a line from the standard input stdin and store it in the string pointed to by str. It stops when a newline is read, or when the end of the file is reached.
Function return value:
If successful, the function returns str. Returns NULL if an error occurs or if the end of the file is reached and no characters have been read.

   char str1[15];
   printf("请输入一个字符串:")
   gets(str1);
   printf("输入的字符串是:%s", str1);

6.5.3 cracked

strcat (character array 1, character array 2):
concatenate the strings in two character arrays, connect string 2 to string 1, put the result in character array 1, and get a value after the function call – character array 1 the address of

char str1[30] = {
    
    "People's Republic of "};
char str2[] = {
    
    "Chana"};
printf("%s",strcat(str1,str2));
//输出:People's Republic of Chana

6.5.4 strcpy

strcpy (character array 1, character array 2):
string copy function, copy string 2 to character array 1, copy the string starting from the address of character array 2 and containing the NULL terminator to the string starting from character array 1 address space

char str1[10]char str2[] = {
    
    "Chana"};
strcpy(str1,str2);

6.5.5 strcmp

strcmp (character array 1, character array 2)
compares two strings character by character from left to right (compared by ASCII code size), until different characters appear or "\0" is encountered.
Comparison result:
String 1 = String 2, the function value is 0
String 1 > String 2, the function value is a positive integer
String 1 < String 2, the function value is a negative integer

6.5.6 str

strlen (character array)
function to test the length of the string, the function value is the actual length in the string, excluding "\0"

char str[10] = {
    
    "Chana"};
printf("%d",strlen(str));
//结果为5

6.5.7 strlwr and strupr

strlwr (character array): convert uppercase letters to lowercase letters, others do not convert
strupr (character array): convert lowercase letters to uppercase letters, others do not convert
Note: strlwr() and strupr() are not standard library functions, It can only be used under windows (VC, MinGW, etc.), and needs to be defined by itself in Linux GCC.

6.6 Application example of string array

1) Output a pattern
insert image description here

#include <stdio.h>
int main(){
    
    
	char diamond[][5] = {
    
    {
    
    ' ',' ','*'},{
    
    ' ','*',' ','*'},{
    
    '*',' ',' ',' ','*'},{
    
    ' ','*',' ','*'},{
    
    ' ',' ','*'}};
	int i, j;
	for(i = 0; i < 5; i++){
    
    
		for(j = 0; j < 5; j++){
    
    
			printf("%c", diamond[i][j]);
		}
		printf("\n");
	} 
}

2. Enter a line of characters, count how many words there are, and separate words with spaces

#include <stdio.h>
int main(){
    
    
	char string[100];
	int i, num = 0, word = 0;
	char c;
	gets(string);
	for(i = 0; (c = string[i]) != '\0'; i++){
    
    
		if(c == ' ') word = 0;
		else if(word == 0){
    
    
			num++;
			word = 1;			
		}
	}
	printf("there are %d words in the line.\n", num);
}

3. There are three strings, find the largest among them

#include <stdio.h>
#include <string.h> 
int main(){
    
    
	char str1[20];
	char str2[3][20];
	int i;
	for(i = 0; i < 3; i++){
    
    
		gets(str2[i]);
	}
	if(strcmp(str2[0],str2[1]) > 0) strcpy(str1,str2[0]);
	else strcpy(str1, str2[1]);
	if(strcmp(str2[2], str1) > 0) strcpy(str1, str2[2]);
	printf("\n the largest string is : \n%s\n",str1);
}

Guess you like

Origin blog.csdn.net/qq_43310387/article/details/123967704