26
#&%$'I (13) !'"% &'

#& %$' I (13)#include intmain(void) {inta, b[4]; char str[10] = "Hello!"; printf("size of intis %ld¥n", sizeof(int)); printf("size of a is %ld¥n", sizeof(a)); printf

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

  • I(13)

  • –––

    •–

  • #define GRIDSIZE 8

    int main(void) {int grid[GRIDSIZE][GRIDSIZE];int x, y;

    for(y = 0; y < GRIDSIZE; y++) {for(x = 0; x < GRIDSIZE; x++) {grid[y][x] = 0;

    }}grid[3][3] = 1;grid[3][4] = -1;grid[4][3] = -1;grid[4][4] = 1;

    . . .

    [8][8][]

    0 1 2 3 4 5 6 7 àx012

    ↓ 3y 4

    567

    1

    1

    -1

    -1

    0

    1-1

    ex1.c

  • • [] – int array[4][3];

    –array[y][x] = 1;printf("data = %d¥n", array[2][2]);scanf("%d", &array[j][i]);

    •array[4,3];

  • void showBoard(int g[GRIDSIZE][GRIDSIZE]);

    void showBoard(int g[GRIDSIZE][GRIDSIZE]) {int x, y;

    for(y = 0; y < GRIDSIZE; y++) {for(x = 0; x < GRIDSIZE; x++) {if(g[y][x] == 1)printf("* ");else if(g[y][x] == -1)printf("o ");elseprintf(". ");

    }printf("¥n");

    }}

    . . . . . . . .

    . . . . . . . .

    . . . . . . . .

    . . . * o . . .

    . . . o * . . .

    . . . . . . . .

    . . . . . . . .

    . . . . . . . .

    ex1.c

  • []•

    void func(int array[8][8]) { ... }void func(int array[][8]) { ... }

    •void func(int array[][]) { ... }

    []

  • • {} –

    • {} int data[3][2] = {

    { 4, 5 },{ 2, 3 },{ 1, 7 }

    };

    0 1

    0 4 5

    1 2 3

    2 1 7

    ••

  • #include

    int main(void) {int data[3][2] = {{ 4, 5 },{ 2, 3 },{ 1, 7 }

    };int x, y;

    for(y = 0; y < 3; y++) {for(x = 0; x < 2; x++) {printf("data[%d][%d] = %d¥n", y, x, data[y][x]);

    }}return 0;

    }

    data[0][0] = 4data[0][1] = 5data[1][0] = 2data[1][1] = 3data[2][0] = 1data[2][1] = 7

    ex2.c

    data

  • (1)

    1004

    1005

    1006

    1007

    1008

    1009

    1010

    1011

    1012

    1013

    1014

    1015

    int a, b;char c, d;

    int a;

    int b;

    char c;

    char d;

    int 32bit(4 )char 8bit

    a

    b

    c

    d

  • (2)

    • 1004 01005 0

    1006 0

    1007 0

    1008

    1009

    1010

    1011

    1012 65

    1013

    1014

    1015

    a = 0;c = 'A';'A' 65

    a

    b

    c

    d

    1

  • • scanf%

    1004 0

    1005 0

    1006 0

    1007 0

    1008

    1009

    1010

    1011

    1012 65

    1013

    1014

    1015

    scanf("%d", &b);scanf("%c", &d);

    • &

    • scanf

    a

    b

    c

    d

    %d int%c char 1

    1

  • • 10041005

    1006

    1007

    1008

    1009

    1010

    1011

    1012 1

    1013 0

    1014 0

    1015 0

    int x[3];x[2] = 1;

    • x 1004• x int 4• x[2] 2 2

    x[0]

    x[1]1004 + 2 * 4 = 1012

    x[2]

  • • 21004

    1005

    1006

    1007

    1008

    1009

    1010

    1011

    1012 1

    1013 0

    1014 0

    1015 0

    1016 2

    1017 0

    1018 0

    1019 0

    1020

    1021

    1022

    int x[3][2];x[1][0] = 1;x[1][1] = 2;

    • 3

    x[0][0]

    1004 + (1 * 2 + 0) * 4 = 10121004 + (1 * 2 + 1) * 4 = 1016

    x[0][1]

    x[1][0]

    x[1][1]

    x[2][0]

  • []•

    void func(int array[8][8]) { ... }void func(int array[][8]) { ... }

    •void func(int array[][]) { ... }

    []

    Q: [] A:

  • • sizeof()––

    •sizeof(int) sizeof(float)

    •int b; char str[10];sizeof(b) sizeof(str)

  • #include

    int main(void) {int a, b[4];char str[10] = "Hello!";

    printf("size of int is %ld¥n", sizeof(int));printf("size of a is %ld¥n", sizeof(a));printf("size of int[4] is %ld¥n", sizeof(int [4]));printf("size of b is %ld¥n", sizeof(b));printf("size of str is %ld¥n", sizeof(str));

    return 0;}

    size of int is 4size of a is 4size of int[4] is 16size of b is 16size of str is 10

    ex3.c

  • •float a, b = 1.5;a = (int)b;

    – b 1.5) int– a 1.0

    •cast : ( )

  • #include

    int main(void) {float a, b = 1.5;int c = 3;

    a = b;printf("%f¥n", a);a = (int)b;printf("%f¥n", a);

    a = 1/c;printf("%f¥n", a);a = 1/3.0;printf("%f¥n", a);a = 1/(float)c;printf("%f¥n", a);

    return 0;}

    1.5000001.0000000.0000000.3333330.333333

    ex4.c

  • – ( 1) ? ( 2) : ( 3)• 1 0 2

    3•

    •––

  • (1)

    • 38–

    • turn +1 -1

    • 41–

    • 42-48–

  • (2)

    • 51-61–

    ––

    53• 64-70

    –– human compRandom

  • (3)

    • 83 initBoard()–

    • 99 showBoard()–

    %-2d• #define

  • (4)

    • 125 dir[]–

    0-7• 137 isPlaceOK()

    – role

  • (5)

    • 166 place()–– isPlaceOK()

    • 201 shouldPass()– isPlaceOK()

    • 215 human()––

  • (6)

    • 223 compRandom()––

    • 239 countNum()–

    ••

  • •––

    ••

    ––

    place() isPlaceOK()