实验四

实验四

实验一:

源代码:

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

 

运行结果:

问题:

实验二:

源代码:

 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         printf("ans=%.2f\n\n",ans);
13     }
14     return 0;    
15 } 
16 void input(int x[],int n){
17     int i;
18     for(i=0;i<n;++i)
19         scanf("%d",&x[i]);
20 }
21 double compute(int x[],int n){
22     int i,high,low;
23     double ans;
24     high=low=x[0];
25     ans=0;
26     for(i=0;i<n;++i){
27         ans+=x[i];
28         if(x[i]>high)
29         high=x[i];
30         else if(x[i]<low)
31         low=x[i];
32     }
33     ans=(ans-high-low)/(n-2);
34     return ans;
35 }

 

运行结果:

问题:

实验三:

源代码:

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

 

运行结果:

 

问题:

实验四:

源代码:

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

 

运行结果:

实验五

源代码:

 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("输入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     return 0;
18 }
19 void input(int x[][N], int n) {
20     int i, j;  
21     for (i = 0; i < n; ++i) {
22         for (j = 0; j < n; ++j)
23             scanf("%d", &x[i][j]);
24     }
25 }
26 void output(int x[][N], int n) {
27     int i, j;
28     for (i = 0; i < n; ++i) {
29         for (j = 0; j < n; ++j)
30             printf("%4d", x[i][j]);
31         printf("\n");
32     }
33 }
34 void rotate_to_right(int x[][N],int n){
35     int i,j,temp[N];
36     for(i=0;i<n;i++){
37         temp[i]=x[i][n-1];
38     }
39     for(j=n-1;j>0;j--){
40         for(i=0;i<n;i++){
41             x[i][j]=x[i][j-1];
42         }
43     }
44     for(i=0;i<n;i++){
45         x[i][0]=temp[i];
46     }
47 }

 

运行结果:

实验六

源代码:

 1 lude <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     int i = 0, temp = x;
18     int result[N] = {0}; 
19     if (x == 0) {
20         printf("0");
21         return;
22     }
23     while (temp > 0) {
24         int remainder = temp % n;
25         result[i++] = remainder;
26         temp /= n;
27     }
28     for (int j = i - 1; j >= 0; j--) {
29         if (n == 16) { 
30             if (result[j] >= 10) {
31                 printf("%c", 'A' + result[j] - 10);
32             } else {
33                 printf("%d", result[j]);
34             }
35         } else {
36             printf("%d", result[j]);
37         }
38     }

 

运行结果:

实验七

源代码:

 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 int is_magic(int x[][N], int n);
 6 int main() {
 7     int x[N][N];
 8     int n;
 9     while (printf("输入n:"), scanf("%d", &n) != EOF) {
10         if (n % 2 == 0 || n <= 0 || n > N) {
11             printf("n 必须是一个大于0且不超过 %d 的奇数\n", N);
12             continue;
13         }
14         printf("输入方阵:\n");
15         input(x, n);
16         printf("输出方阵:\n");
17         output(x, n);
18         if (is_magic(x, n))
19             printf("是魔方矩阵\n\n");
20         else
21             printf("不是魔方矩阵\n\n");
22     }
23     return 0;
24 }
25 void input(int x[][N], int n) {
26     int i, j;
27     for (i = 0; i < n; i++) {
28         for (j = 0; j < n; j++) {
29             scanf("%d", &x[i][j]);
30         }
31     }
32 }
33 void output(int x[][N], int n) {
34     int i, j;
35     for (i = 0; i < n; i++) {
36         for (j = 0; j < n; j++) {
37             printf("%4d", x[i][j]);
38         }
39         printf("\n");
40     }
41 }
42 int is_magic(int x[][N], int n) {
43     int sum = 0, i, j, row_sum, col_sum, diag_sum1, diag_sum2;
44     for (i = 0; i < n; i++) {
45         sum += x[0][i];
46     }
47     for (i = 1; i < n; i++) {
48         row_sum = 0;
49         for (j = 0; j < n; j++) {
50             row_sum += x[i][j];
51         }
52         if (row_sum != sum) {
53             return 0;
54         }
55     }
56     for (j = 0; j < n; j++) {
57         col_sum = 0;
58         for (i = 0; i < n; i++) {
59             col_sum += x[i][j];
60         }
61         if (col_sum != sum) {
62             return 0;
63         }
64     }
65     diag_sum1 = 0;
66     diag_sum2 = 0;
67     for (i = 0; i < n; i++) {
68         diag_sum1 += x[i][i];
69         diag_sum2 += x[i][n - i - 1];
70     }
71     if (diag_sum1 != sum || diag_sum2 != sum) {
72         return 0;
73     }
74     return 1;
75 }

 

运行结果:

 

posted on 2024-11-10 13:54  安宁的空白  阅读(2)  评论(0编辑  收藏  举报