Oct. 23.
Off-topic topic: Compare three numbers and output.
//Enter three different integers and compare sizes to output from large to small #include<stdio.h> int main() { int a,b,c; printf("Enter three different integers:\n"); scanf("%d %d %d",&a,&b,&c); //Three integers int t; if(b>a) //Compare Size Process { t = a; a = b; b = a; } if(a<c) { t = a; a = c; c = t; } if(c>b) { t = b; b = c; c = t; } //End of comparison printf("%d > %d > %d\n",a,b,c); return 0; }
array
One-dimensional array
Definition
Type specifier array identifier [constant expression];
int i[5];
Note, for example, that an assignment in i[5] contains five elements, i[0], i[1], i[2], i[3], i[4], and there is no i[5]. If there is an error, the subscript crosses the boundary.
Quote
Array identifier [subscript];
i[3];
char c[5];
Note that subscripts can be integer data or integer expressions and are automatically rounded if the result is a decimal number.
Initialization
Can be assigned directly
int i[5] = {1,2,3,4,5};
or
int i[6] = {1,2,3};
or
char c[3] = {'a','b','d'};
There will then be five variables available. Unwritten only assigns the first part, and subsequent parts default to 0.
//Simply define and assign a one-dimensional array, then output the data in reverse order #include<stdio.h> int main() { int Mem[5],i = 1; printf("Please enter five data:"); for(i = 0;i <= 4;i ++) //Input data { scanf("%d",&Mem[i]); } for(i = 0;i <= 4;i ++) //Show Array Elements { printf("%d",Mem[i]); } printf("\n"); //Line Break int j; for(i = 0;i <= 2;i ++) //In reverse order, the third variable is used for data exchange purposes. { j = Mem[i]; Mem[i] = Mem[4-i]; Mem[4-i] = j; } for(i = 0;i <= 4;i ++) //Output Reverse Order Results { printf("%d",Mem[i]); } return 0; }
The code above uses the intermediate variable j for data exchange in reverse order, which is similar to the idea of the topic "Comparing the sizes of three numbers and outputting".
When quickly outputting all elements of an array at once, you must now use a for loop.
2-D Array
A two-dimensional array defines the same declaration as a one-dimensional array:
Data type array name [constant expression 1] [constant expression 2];
int i[5][6];
Where expression 1 is the row subscript and expression 2 is the column subscript.
The rest is roughly like a one-dimensional array.
As shown above, we can know that the array has 5*6 subscript variables, i[0][0], i[0][1], i[0][1], i[0][1], i[0][2], i[0][3], i[0][3], i[0][0], i[1][0], i[1][0] [0], i[1][1][3], i[1][4], i[1][4], i[1][1][4], i[1][1][1][1][1][1] [1] [1], i[1][1][4], i[1][1][4], i[1][1][1], i[][4] etc.
In a two-dimensional array, data is arranged in rows, each row being stored in turn, and one row ends with the next.
Initialization
There are four cases
I.
int i[2][2] = {1,2,3,4}; // Line by Line + Assignment in Order
2.
int j[][2] = {1,2,3,4,5,6}; // Omit row subscript
3.
int k[3][3] = {{1,3,5},{2,4,6}}; // Branch assignment
int l[2][3] = {{1,3},{4,5}}; // Only part of the value is assigned here, defaulting to 0 for the part assigned
V.
int i[2][3];
i[0][1] = 7; // Assignment can be done with a for loop
Trying to store data using a two-dimensional array
#include<stdio.h> int main() { int a[2][2],b[2][2]; //Define Array for(int i = 0;i < 2;i ++) //for loops nested to assign values to arrays by keyboard { for(int j = 0;j < 2;j ++) { printf("a[%d][%d]=",i,j); scanf("%d",&a[i][j]); } } printf("Output two-dimensional array:\n"); //Output Tips for(int i = 0;i < 2;i ++) //for loop nested to output array { for(int j = 0;j < 2;j ++) { printf("%d\t",a[i][j]); } printf("\n"); //Next row array wrap } return 0; }