1. Two-dimensional array
1). Definition and reference of two-D arrays
When assigning initial values to all elements of an array, the number of rows cannot be omitted, but the number of columns can be omitted
Omit line length
The initial value is assigned to all elements.
int a[][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Or branch is assigned an initial value, all rows are listed in the initial value table
static int b[][3] = {{1, 2, 3}, {}, {4, 5}, {}}3). Programming with two-dimensional arrays
<1>. Matrix Transpose
Enter a positive integer n (1<n≤6), generate 1 N-n square matrices according to the following formula, then transpose the Phalanx (row and column interchange) and output.
A[I][J] = i n + j + 1 (0≤i≤n-1,0≤j≤n-1)
Analysis: int a[6][6]; When n=3
The code is:
#include <stdio.h>int main(void){ int i, j, n, temp; int a[6][6]; printf ( "Enter n: “ ); scanf ( "%d", &n ); /* 给二维数组赋值 略…… */ /* 行列互换 */ for ( i = 0; i < n; i++ ) for ( j = 0; j < n; j++ ) if ( i <= j ){ /* 只遍历上三角阵 */ temp = a[i][j]; a[i][j] = a[j][i]; a[j][i] = temp; } /* 按矩阵的形式输出a 略…… */
<2> Calculate date
Code:
<font size=5 color="black">int year, month, day; int k, leap; int tab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; scanf("%d %d %d", &year, &month, &day); leap = (year % 4 == 0 && year%100!=0 || year%400==0); for(k = 1; k < month; k++) { day = day + tab[leap][k]; } printf("day = %d\n", day) ;
2. One-dimensional character array
1). String