《C和指针》章节后编程练习解答参考——第8章

8.1

 

  1 #include <stdio.h>
  2 
  3 int main (void)
  4 {
  5     int a, b, c, d;
  6 
  7     // 不使用嵌套花括号初始化
  8     unsigned char char_value[3][6][4][5] = 
  9     {
 10         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,    // 0,0,X,X
 11         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,    // 0,1,X,X
 12         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,
 13         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,
 14         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,
 15         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,
 16 
 17         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,    // 1,0,X,X
 18         0,    0,    0,    0,    0,            0,    ' ',    0,    0,    0,        0,    0,    0,    0,    0,            0,    0,    0,    0,    0,    // 1,1,X,X
 19         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    'A',    0,        0,    0,    0,    0,    'x',
 20         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0xf3,    0,    0,        0,    0,    0,    0,    0,
 21         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    '\n',    0,        0,    0,    0,    0,    0,
 22         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,
 23 
 24         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,    // 2,0,X,X
 25         0,    0,    0,    0,    0,            0,    0,    0320,    0,    0,        0,    0,    0,    0,    0,            0,    0,    0,    0,    0,    // 2,1,X,X
 26         0,    0,    0,    0,    0,            0,    '0',    0,    0,    0,        0,    0,    '\'',    0,    0,        0,    '\121',    0,    0,    0,
 27         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,
 28         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    '3',    3,    0,
 29         0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    0,            0,    0,    0,    0,    125,
 30     };
 31 
 32 /*
 33     // 使用嵌套花括号初始化
 34     unsigned char char_value[3][6][4][5] = 
 35     {
 36         {    //0,X,X,X
 37             {    //0,0,X,X
 38                 { 0 },    //0,0,0,X    
 39             }
 40         },
 41         {    //1,X,X,X
 42             { 0 },    //1,0,X,X
 43             {    //1,1,X,X
 44                 { 0 },    //1,1,0,X
 45                 {    //1,1,1,X
 46                     0, ' '
 47                 }    
 48             },
 49             {    //1,2,X,X
 50                 { 0 },    //1,2,0,X
 51                 { 0 },    //1,2,1,X
 52                 { 0, 0, 0, 'A' },//1,2,2,X
 53                 {    //1,2,3,X
 54                     0, 0, 0, 0, 'x'
 55                 }        
 56             },
 57             {    //1,3,X,X
 58                 { 0 },    //1,3,0,X
 59                 { 0 },    //1,3,1,X
 60                 {    //1,3,2,X
 61                     0, 0, 0xf3
 62                 }    
 63             },
 64             {    //1,4,X,X
 65                 { 0 },    //1,4,0,X
 66                 { 0    },//1,4,1,X
 67                 {    //1,4,2,X
 68                     0, 0, 0, '\n'
 69                 }    
 70             }    
 71         },
 72         {    //2,X,X,X
 73             { 0 },    //2,0,X,X
 74             {    //2,1,X,X
 75                 { 0 },    //2,1,0,X
 76                 {    //2,1,1,X
 77                     0, 0, 0320
 78                 }        
 79             },
 80             {    //2,2,X,X
 81                 { 0 },    //2,2,0,X
 82                 {    //2,2,1,X
 83                     0, '0'
 84                 },
 85                 {    //2,2,2,X
 86                     0, 0, '\''
 87                 },
 88                 {    //2,2,3,X
 89                     0, '\121'
 90                 }        
 91             },
 92             { 0 },    //2,3,X,X
 93             {    //2,4,X,X
 94                 { 0 },    //2,4,0,X
 95                 { 0 },    //2,4,1,X
 96                 { 0 },    //2,4,2,X
 97                 {    //2,4,3,X
 98                     0, 0, '3', 3
 99                 }        
100             },
101             {    //2,5,X,X
102                 { 0 },//2,5,0,X    
103                 { 0 },//2,5,1,X    
104                 { 0 },//2,5,2,X                
105                 {    //2,5,3,X
106                     0, 0, 0, 0, 125
107                 }        
108             }        
109         }    
110     };
111 */
112 
113     for ( a = 0; a < 3; a++ )
114         for ( b = 0; b < 6; b++ )
115             for ( c = 0; c < 4; c++ )
116                 for ( d = 0; d < 5; d++ )
117                 {
118                     if ( char_value[a][b][c][d] != 0 )
119                         printf("char_value[%d][%d][%d][%d] = %d\n", a, b, c, d, char_value[a][b][c][d]);
120                 }
121 
122     getchar();
123     return 0;
124 }


8.3

 

 1 #include <stdio.h>
 2 
 3 #define N 3
 4 
 5 #define  TRUE  1
 6 #define  FLASE  0
 7 
 8 int
 9 identity_matrix(int mat[][N])
10 {
11     int i, j;    
12     for( i = 0; i < N; i++)
13     {
14         for( j = 0; j < N; j++)
15         {
16             if ( ( i == j ) && ( 1 != mat[i][j] ) )
17                 return FLASE;
18             if ( ( i != j ) && ( 0 != mat[i][j] ) )
19                 return FLASE;
20         }
21     }
22     return TRUE;
23 }
24 
25 void show_mat(int mat[][N])
26 {
27     int i, j;
28     for ( i = 0; i < N; i++)
29     {
30         for( j = 0; j < N; j++)
31         {
32             printf("%d\t", mat[i][j]);
33         }
34         putchar('\n');
35     }
36 }
37 
38 int main (void)
39 {
40     int mat[][N] = 
41     { 
42         { 1 },
43         { 0, 1 },
44         { 0, 0, 1}
45     };
46     
47     int state = identity_matrix(mat);
48     show_mat(mat);
49 
50     if ( state )
51     {
52         printf("Yes!\n");
53     }
54     else
55         printf("No!\n");
56 
57     getchar();
58     return 0;
59 }

 

8.4

 

 1 #include <stdio.h>
 2 
 3 #define  N  3
 4 
 5 #define  TRUE  1
 6 #define  FLASE  0
 7 
 8 int identity_matrix(int *mat, int n)    //判断矩阵是否为单位矩阵
 9 {
10     int i, j;    
11     for( i = 0; i < n; i++)
12     {
13         for( j = 0; j < n; j++)
14         {
15             if ( ( i == j ) && ( 1 != *mat ) )
16                 return FLASE;
17             if ( ( i != j ) && ( 0 != *mat ) )
18                 return FLASE;
19             mat++;
20         }
21     }
22     return TRUE;
23 }
24 
25 void show_mat(int *mat, int n)    //打印矩阵元素
26 {
27     int i, j;
28 
29     for ( i = 0; i < n; i++)
30     {
31         for( j = 0; j < n; j++)
32         {
33             printf("%d\t", *mat++);
34         }
35         putchar('\n');
36     }
37 }
38 
39 int main (void)
40 {
41     int mat[][N] = 
42     { 
43         { 1 },
44         { 0, 1 },
45         { 0, 0, 1}
46     };
47     
48     int state = identity_matrix( &mat[0][0], N);
49     show_mat(&mat[0][0], N);
50 
51     if ( state )
52     {
53         printf("Yes!\n");
54     }
55     else
56         printf("No!\n");
57 
58     getchar();
59     return 0;
60 }

 

8.5

 

 1 #include <stdio.h>
 2 
 3 #define  X  3
 4 #define  Y  2
 5 #define  Z  4
 6 
 7 void matrix_multiply(int *m1, int *m2, int *r, int x, int y, int z)
 8 {
 9     int i, j, k, sum;
10 
11     for ( i = 0; i < x; i++ )
12     {
13         for ( k = 0; k < z; k++ )
14         {
15             sum = 0;
16             for ( j = 0; j < y; j++ )
17             {
18                 sum += (*(m1+(i*y)+j)) * (*(m2+(j*z+k)));
19             }
20             *( r + (i*z) + k ) = sum;
21         }
22     }
23 }
24 
25 void show_mat(int *mat, int x, int y)    //打印矩阵元素
26 {
27     int i, j;
28 
29     for ( i = 0; i < x; i++)
30     {
31         for( j = 0; j < y; j++)
32         {
33             printf("%d\t", *mat++);
34         }
35         putchar('\n');
36     }
37     putchar('\n');
38 }
39 
40 int main (void)
41 {
42     int m1[X][Y] = 
43     {
44         { 2, -6 },
45         { 3,  5 },    
46         { 1, -1 }        
47     };
48 
49     int m2[Y][Z] =
50     {
51         { 4, -2, -4, -5 },
52         { -7, -3, 6, 7 }    
53     };
54 
55     int r[X][Z] = { 0 };
56 
57     printf("m1 = \n");
58     show_mat(&m1[0][0], X, Y);
59     printf("m2 = \n");
60     show_mat(&m2[0][0], Y, Z);
61 
62     matrix_multiply(&m1[0][0], &m2[0][0], &r[0][0], X, Y, Z);
63     printf("r = m1 * m2 = \n");
64     show_mat(&r[0][0], X, Z);
65 
66     getchar();
67     return 0;
68 }

 

转载于:https://www.cnblogs.com/microxiami/p/5028234.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值