实验四

实验任务1:

源代码:

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

回答问题:

1.不是连续存放的,

实验任务2:

源代码:

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

 回答问题:函数input的功能是输出要输入的个数与数据,函数compute的功能是去除最大值和最小值后求平均数

实验任务3:

源代码:

 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 
20 void output(int x[][N],int n){
21     int i,j;
22     
23     for(i=0;i<n;++i){
24         for(j=0;j<n;++j)
25          printf("%d",x[i][j]);
26         printf("\n");
27     }
28 }
29 
30 void init(int x[][N],int n,int value){
31     int i,j;
32     
33     for(i=0;i<n;++i)
34      for(j=0;j<n;++j)
35       x[i][j]=value;
36 }

回答问题:

1.第二维不能省略

2.函数output的功能是将二维数组输出,函数init的功能是将输入的value的值赋给二维数组

实验任务4:

源代码:

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

 

 实验任务5:

源代码:

 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 void rotate_to_right(int x[][N],int n);
 7 
 8 int main(){
 9     int x[N][N];
10     int n;
11     
12     printf("Enter n: ");
13     scanf("%d",&n);
14     input(x,n);
15     
16     printf("原始矩阵:\n");
17     output(x,n);
18     
19     rotate_to_right(x,n);
20     
21     printf("变换后矩阵:\n");
22     output(x,n);
23     
24     return 0;
25 }
26 
27 void input(int x[][N],int n){
28     int i,j;
29     
30     for(i = 0; i < n; ++i){
31         for(j = 0;j<n;++j)
32           scanf("%d",&x[i][j]);
33     }
34 }
35 
36 void output(int x[][N], int n){
37     int i,j;
38     
39     for(i = 0;i < n;++i){
40         for(j = 0; j<n; ++j)
41           printf("%4d",x[i][j]);
42           
43         printf("\n");
44     }
45 }
46 
47 void rotate_to_right(int x[][N],int n){
48     int t,j,i;
49     for (i = 0; i < n; i++) {
50         t = x[i][n-1];
51         for (j = n - 1; j > 0; j--) {
52             x[i][j] = x[i][j-1];
53         }
54         x[i][0] = t;
55     }
56 }

 实验任务6:

源代码:

 1 #include <stdio.h>
 2 #define N 100
 3 
 4 void dec_to_n(int x, int n);
 5 
 6 int main(){
 7     int x;
 8     
 9     while(printf("输入十进制整数: "),scanf("%d",&x) !=EOF){
10         dec_to_n(x, 2);
11         dec_to_n(x, 8);
12         dec_to_n(x, 16);
13         
14         printf("\n");
15     }
16     
17     return 0;
18 }
19 
20 void dec_to_n(int x, int n){
21     int a[100];
22     int i=0;
23     int m;
24     if(n==2){
25         while(x!=0){
26             m=x%2;
27             a[i++]=m;
28             x/=2;
29         }
30     }
31     else if(n==8){
32         while(x!=0){
33             m=x%8;
34             a[i++]=m;
35             x/=8;
36         }
37     }
38     else if(n==16){
39         while(x!=0){
40             m=x%16;
41             if(m<10)
42               a[i++]=m;
43             else
44               a[i++]=m-10+'A';
45             x/=16;
46     }
47     }
48     for (int j = i - 1; j >= 0; j--) {
49         if (a[j] < 10) {
50             printf("%d", a[j]);
51         } else {
52             printf("%c", (char)a[j]);
53         }
54     }
55     printf("\n");
56 }

 

 实验任务7:

源代码:

 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 
 8 int main() {
 9     int x[N][N];
10     int n;
11     
12     while(printf("输入n: "), scanf("%d", &n) != EOF){
13         printf("输入方阵:\n");
14         input(x, n);
15         
16         printf("输出方阵:\n");
17         output(x, n);
18         
19         if(is_magic(x, n))
20             printf("是魔方矩阵\n\n");
21         else
22             printf("不是魔方矩阵\n\n");
23     }
24     
25     return 0;
26 }
27 
28 void input(int x[][N], int n) {
29     int i, j;
30     for (i = 0; i < n; ++i) {
31         for (j = 0; j < n; ++j)
32             scanf("%d", &x[i][j]);
33     }
34 }
35 
36 void output(int x[][N], int n) {
37     int i, j;
38     for (i = 0; i < n; ++i) {
39         for (j = 0; j < n; ++j)
40             printf("%4d", x[i][j]);
41              
42         printf("\n");
43     }
44 }
45 int is_magic(int x[][N], int n){
46     int t = n * (n * n + 1) / 2; 
47     
48     int i, j;
49     for (i = 0; i < n; i++) {
50         int a = 0;
51         for (j = 0; j < n; j++) {
52             a += x[i][j];
53         }
54         if (a!= t) {
55             return 0;  
56         }
57     }
58     
59     for (j = 0; j < n; j++) {
60         int b = 0;
61         for (i = 0; i < n; i++) {
62             b += x[i][j];
63         }
64         if (b!= t) {
65             return 0;  
66         }
67     }
68     
69     int c = 0;
70     for (i = 0; i < n; i++) {
71         c += x[i][i];
72     }
73     if (c!= t) {
74         return 0;  
75     }
76     
77     int d = 0;
78     for (i = 0; i < n; i++) {
79         d += x[i][n - 1 - i];
80     }
81     if (d!= t) {
82         return 0;  
83     }
84     
85     return 1;
86 }

 

 

posted @ 2024-11-10 18:15  雅ya  阅读(3)  评论(0编辑  收藏  举报