15-week job (two-dimensional array)

Source: Internet
Author: User

Summary of learning content this week

    1. Two-dimensional array

    1). Definition and reference of two-D arrays

  • Defined
    Type an array group name [President degree] [column length]

    int a[3][2];
    Defines a two-dimensional array a,3 rows 2 columns, 6 elements

    int b[5][10];
    Defines a two-dimensional array b,5 rows 10 columns, 50 elements

  • Reference
    Define first, then use
    References to array elements:
    Array name [row subscript] [column subscript]
    Row Subscript and column subscript: int expression
    The range of row subscript is [0, line length-1]
    Column subscript value range is [0, column length-1]

    int a[3][2]; 3 Rows, 2 columns, 6 elements
    A[0][0] a[0][1]
    A[1][0] a[1][1]
    A[2][0] a[2][1]

    2). Initialization of two-D arrays

  • Initial value of branch assignment
    int A[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    static int B[4][3] = {{1, 2, 3}, {}, {4, 5}};

    Array A
    1 2 3
    4 5 6
    7 8 9

    Array b
    1 2 3
    0 0 0
    4 5 0
    0 0 0

  • Sequential assignment of initial values
    int a[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    static int B[4][3] = {1, 2, 3, 0, 0, 0, 4, 5};

  • 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

  • The storage and operation of strings can be implemented with a one-dimensional character array
  • The definition, reference, and initialization of one-dimensional character arrays is the same as other types of one-dimensional arrays.
    Char str[80];
    Defines an array of 80-character elements str
    Char t[5]={' H ', ' a ', ' P ', ' P ', ' Y '};
    Initializing an array of T

    1). String

  • Storage of strings-array initialization
    Strings can be stored in a one-dimensional character array
    static char s[6] = {' H ', ' a ', ' P ', ' P ', ' Y ', ' n '};
    Character array initialization: using string constants
    static char s[6] = {"Happy"};
    static char s[6] = "Happy";
    Array length ≥ string valid length + 1
    Char t[5]; Can "Happy" be deposited in T?

  • Storage of strings

  • Operations on strings

  • Storage of strings-assignment and input

  • Programming with strings

    Learning process this week

15-week job (two-dimensional array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.