4

实验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 
10 printf("sizeof(x) = %d\n", sizeof(x));
11 
12 
13 for (i = 0; i< N; ++i)
14     printf("%p: % d\n",&x[i], x[i]);
15 
16 
17 printf("x = %d\n", x);
18 }
19 
20 void test2() {
21 int x[M][N] = {{1, 9, 8, 4},{2, 0, 4, 9}};
22 int i,j;
23 
24 
25 printf("sizeof(x) = %d\n", sizeof(x));
26 
27 
28 for(i = 0; i < M; ++i)
29    for(j = 0; j < N; ++j)
30        printf("%p: %d\n", &x[i][j], x[i][j]);
31 printf("\n");
32 
33 
34 printf("x = %p\n", x);
35 printf("x[0] = %p\n", x[0]);
36     printf("x[1] = %p\n", x[1]);
37     printf("\n");
38 }
39 
40 int main() {
41     printf("测试1: int型一维数组\n");
42     test1();
43     
44     printf("\n测试2: int型二维数组\n");
45     test2();
46     
47     return 0;
48 }

实验2

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

实验3:

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

第一维

输入i*j的数据

输出n*n的数据

实验5

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

实验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 while(printf("输入十进制整数: "), scanf("%d", &x) != EOF) {
 9 dec_to_n(x, 2);
10 dec_to_n(x, 8);
11 dec_to_n(x, 16);
12 printf("\n");
13 }
14 
15 return 0;
16 }
17 
18 void dec_to_n(int x,int n){
19      int a[N],i,j=0;
20      for(i=x;i!=0;i/=n){
21          a[j]=x%n;
22         x/=n;
23         j++;
24      }
25      for(i=j-1;i>=0;i--){
26          if(a[i]%n>9)
27              printf("%c",a[i]-10+'A');
28          else
29              printf("%c",a[i]+'0');
30      }
31      printf("\n");
32 }

实验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 [][N],int n);
 7 
 8 int main(){
 9     int x[N][N];
10     int n;
11 
12     while(printf("输入:"),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     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 int is_magic(int x[][N],int n){
48     int i,j;
49     int sum1,sum2,sum3=0,sum4=0;
50 
51 
52     for(i=0;i<n;++i){
53         sum1=0,sum2=0;
54 
55         for(j=0;j<n;++j){
56             sum1+=x[i][j];
57             sum2+=x[j][i];
58         }
59 
60         if(sum1!=sum2)
61             return 0;
62     }
63 
64     for(i=0;i<n;++i){
65         sum3+=x[i][i];
66         sum4+=x[i][n-i-1];
67     }
68 
69     if(sum3!=sum4)
70         return 0;
71 
72     return 1;
73 }

 

posted @ 2024-11-10 21:33  九泪纵横  阅读(3)  评论(0编辑  收藏  举报