实验4

 1 #include<stdio.h>
 2 #define N 4
 3 #define M 2
 4 void test1()
 5 {
 6     int x[N] = { 1,9,8,4 };
 7     int i;
 8     printf("sizeof(x)=%d\n", sizeof(x));
 9     for (i = 0; i < N; ++i)
10         printf("%p:%d\n", &x[i], x[i]);
11     printf("x=%p\n", x);
12 }
13 void test2() {
14     int x[M][N] = { {1,9,8,4},{2,0,4,9} };
15     int i, j;
16     printf("sizeof(x)=%d\n", sizeof(x));
17     for (i = 0; i < M; ++i)
18 
19         for (j = 0; j < N; ++j)
20             printf("%p:%d\n", &x[i][j], x[i][j]);
21     printf("\n");
22     printf("x[0]=%p\n", x[0]);
23     printf("x[1]=%p\n", x[1]);
24     printf("\n");
25 
26 }
27 int main() {
28     printf("测试1: int型一维数组\n");
29     test1();
30     printf("\n测试2: int型二维数组\n");
31     test2();
32     return 0;
33 }

问题1,连续存放,一样

问题2,连续存放,字面一样

 1 #include<stdio.h>
 2 #define N 100
 3 void input (int x[],int n);
 4 double compute(int x[],int n);
 5 int main() {
 6 int x[N];
 7 int n,i;
 8 double ans ;
 9 while (printf("Enter n:"),scanf("%d",&n)!=EOF){
10     input(x,n);
11     ans =compute(x,n);
12     ans =compute(x,n);
13     printf("ans=%.2f\n\n",ans);
14 }
15 return 0;
16 }
17 void input(int x[],int n){
18     int i;
19     for (i=0;i<n;i++)
20     scanf("%d",&x[i]);
21 }
22 double compute(int x[],int n){
23     int i,high,low;
24     double ans;
25     high=low =x[0];
26     ans =0;
27     for (i=0;i<n;i++){
28         ans =ans+x[i];
29         if (x[i]>high)
30         high=x[i];
31         else if (x[i]<low)
32         low =x[i];
33     }
34     ans =(ans-high-low)/(n-2);
35     return ans ;
36 }

input 用于输入n个数字
compute 计算这组数除去最高最低值后的平均值

 1 #include <stdio.h>
 2 #define N 100
 3 
 4 void output(int x[][N], int n);
 5 void init(int x[][N], int n, int value);
 6 
 7 int main() {
 8     int x[N][N];
 9     int n, value;
10 
11     while(printf("Enter n and value: "), scanf("%d%d", &n, &value) != EOF) {
12         init(x, n, value);  
13         output(x, n);      
14         printf("\n");
15     }
16 
17     return 0;
18 }
19 void output(int x[][N], int n) {
20     int i, j;
21 
22     for(i = 0; i < n; ++i) {
23         for(j = 0; j < n; ++j)
24             printf("%d ", x[i][j]);
25         printf("\n");
26     }
27 }
28 void init(int x[][N], int n, int value) {
29     int i, j;
30 
31     for(i = 0; i < n; ++i)
32         for(j = 0; j < n; ++j)
33             x[i][j] = value;
34 }

问题1,第二维
问题2,output 用于输出n*n阶的矩阵,init将所有的矩阵中数赋值为value

 

 1 #include<stdio.h>
 2 #define N 100
 3 void input(int x[], int n) {
 4     int i;
 5     for (i = 0; i < n; i++)
 6         scanf("%d", &x[i]);
 7 }
 8 double media(int x[], int n)
 9 {
10     int i, t,j;
11     double s;
12     for (j = 0; j < n-1; j++)
13     {
14         for (i = 0; i < n - j - 1; i++)
15         {
16             if (x[i] > x[i + 1])
17             {
18                 t = x[i + 1];
19                 x[i + 1] = x[i];
20                 x[i] = t;
21             }
22         }
23     }
24     if (n % 2 != 0)
25      s = x[(n - 1) / 2];
26     else s = (x[n / 2] + x[(n / 2) - 1]) / 2.0;
27     return s;
28 }
29 int main()
30 {
31     int x[N];
32     int n;
33     double ans;
34     while (printf("Enter n: "), scanf("%d", &n) != EOF) {
35         input(x, n);
36         ans = media(x, n);
37         printf("ans =%g\n\n", ans);
38     }
39     return 0;
40 }

 1 #include <stdio.h>
 2 #define N 100
 3 void input(int x[][N], int n);
 4 void output(int x[][N], int n);
 5 void rotate_to_right(int x[][N], int n);
 6 int main() {
 7     int x[N][N];
 8     int n;
 9     printf("Enter n: ");
10     scanf("%d", &n);
11     input(x, n);
12     printf("原始矩阵:\n");
13     output(x, n);
14     rotate_to_right(x, n);
15     printf("变换后矩阵:\n");
16     output(x, n);
17 }
18 void input(int x[][N], int n) {
19     int i, j;
20     for (i = 0; i < n; ++i) {
21         for (j = 0; j < n; ++j)
22             scanf("%d", &x[i][j]);
23     }
24 }
25 // 函数定义
26 // 功能: 输出一个n*n的矩阵x
27 void output(int x[][N], int n) {
28     int i, j;
29     for (i = 0; i < n; ++i) {
30         for (j = 0; j < n; ++j)
31             printf("%4d", x[i][j]);
32         printf("\n");
33     }
34 }
35 void rotate_to_right(int x[][N], int n) {
36     int i, j;
37     for (i = n - 1; i >= 0; i--)
38     {
39         for (j = n - 1; j >= 0; j--)
40         {
41             x[i][j + 1] = x[i][j];
42         }
43         x[i][0] = x[i][n];
44     }
45 }

 

 1 #include <stdio.h>
 2 #define N 100
 3 void dec_to_n(int x, int n);
 4 int main() {
 5     int x;
 6     while (printf("输入十进制整数: "), scanf("%d", &x) != EOF) {
 7         dec_to_n(x, 2);
 8         printf("\n");
 9         dec_to_n(x, 8);
10         printf("\n");
11         dec_to_n(x, 16);
12         printf("\n");
13     }
14     return 0;
15 }
16 void dec_to_n(int x, int n)
17 {
18     int abc[32];
19     int s = 0;
20     while (x > 0)
21     {
22         abc[s++] = x % n;
23         x = x / n;
24     }
25     int i;
26     for (i = s-1 ; i >= 0; i--)
27     {
28         if (abc[i] >= 10)
29         {
30             printf("%c", abc[i] - 10 + 'A');
31         }
32         else {
33             printf("%d", abc[i]);
34         }
35     }
36 }

 

 1 #include <stdio.h>
 2 #define N 100
 3 
 4 void input(int x[][N], int n);
 5 void output(int x[][N], int n);
 6 int is_magic(int x[][N], int n);
 7 int main() {
 8     int x[N][N];
 9     int n;
10     while (printf("输入n: "), scanf("%d", &n) != EOF) {
11         printf("输入方阵:\n");
12         input(x, n);
13         printf("输出方阵:\n");
14         output(x, n);
15         if (is_magic(x, n))
16             printf("是魔方矩阵\n\n");
17         else
18             printf("不是魔方矩阵\n\n");
19     }
20     return 0;
21 }
22 void input(int x[][N], int n) {
23     int i, j;
24     for (i = 0; i < n; ++i) {
25         for (j = 0; j < n; ++j)
26             scanf("%d", &x[i][j]);
27     }
28 }
29 
30 void output(int x[][N], int n) {
31     int i, j;
32     for (i = 0; i < n; ++i) {
33         for (j = 0; j < n; ++j)
34             printf("%4d", x[i][j]);
35         printf("\n");
36     }
37 }
38 int is_magic(int x[][N], int n)
39 {
40     int i, j, s, a,m;
41     m = 0;
42     s = 0;
43     for (j = 0; j < n; j++)
44     {
45         s = s + x[0][j];
46     }
47     for (i = 1; i < n; i++)
48     {
49         a = 0;
50         for (j = 0; j < n; j++)
51         {
52             a = a + x[i][j];
53         }
54 
55         if (a == s)
56         {
57             m = m + 0;
58         }
59         else m++;
60     }
61     for (j = 0; j < n; j++)
62     {
63         a = 0;
64         for (i = 0; i < n; i++)
65         {
66             a = a + x[i][j];
67         }
68         if (a == s)
69         {
70             m = m + 0;
71         }
72         else m++;
73     }
74     a = 0;
75     for (i = 0; i < n; i++)
76     {
77         a = a + x[i][i];
78     }
79     if (a == s)
80     {
81         m = m + 0;
82     }
83     else m++;
84     a = 0;
85     for (i=0;i<n;i++)
86     {
87         a = a + x[i][n - 1 - i];
88     }
89     if (a == s)
90     {
91         m = m + 0;
92     }
93     else m++;
94     if (m == 0)
95     {
96         return 1;
97     }
98     else { return 0; }
99 }

 

posted @ 2024-11-10 15:09  zhj910  阅读(1)  评论(0编辑  收藏  举报