实验四
实验任务1:
task1.c源代码:
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) printf("%p:%d\n",&x[i],x[i]); 9 printf("x=%p\n",x); 10 } 11 void test2() { 12 int x[M][N] = {{1,9,8,4},{2,0,4,9}}; 13 int i,j; 14 printf("sizeof(x)=%d\n",sizeof(x)); 15 for(i=0;i<M;++i) 16 for(j=0;j<N;++j) printf("%p;%d\n",&x[i][j],x[i][j]); 17 printf("\n"); 18 printf("x=%p\n",x); 19 printf("x[0]=%p\n",x[0]); 20 printf("x[1]=%p\n",x[1]); 21 printf("\n"); 22 } 23 24 int main() { 25 printf("测试1:int型一维数组\n"); 26 test1(); 27 printf("测试2:int型二维数组\n"); 28 test2(); 29 return 0; 30 }
运行结果:
回答问题:
问题1:是的;是的
问题2:是的;数组名X指向数组的第一个元素,X【0】即数组的第一个元素,而X【0】【0】是数组第一个元素的地址,所以,它们在字面上是一样的;相差16bit,也就是4个元素
实验任务2:
task2.c源代码:
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 { 14 input(x,n); 15 ans = compute(x,n); 16 printf("ans=%.2f\n\n",ans); 17 } 18 return 0; 19 } 20 21 void input (int x[],int n){ 22 int i; 23 for(i=0;i<n;++i) scanf("%d",&x[i]); 24 } 25 double compute(int x[],int n) { 26 int i,high,low; 27 double ans; 28 high = low = x[0]; 29 ans = 0; 30 31 for (i=0;i<n;++i) 32 { 33 ans+=x[i]; 34 if(x[i]>high) high = x[i]; 35 else if(x[i]<low) low = x[i]; 36 } 37 ans=(ans-high-low)/(n-2); 38 return ans; 39 }
运行结果:
回答问题:
函数input的功能是输入数组中每个元素的值;函数compute的功能的是将数组中的最大值和最小值,然后求剩余的元素的平均值
试验任务3:
task3.c源代码:
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 { 9 int x[N][N]; 10 int n,value; 11 12 while(printf("Enter n and value:"),scanf("%d%d",&n,&value)!=EOF) 13 { 14 init(x,n,value); 15 output(x,n); 16 printf("\n"); 17 } 18 return 0; 19 } 20 21 void output(int x[][N],int n) 22 { 23 int i,j; 24 for(i=0;i<n;++i){ 25 for(j=0;j<n;++j) printf("%d",x[i][j]); 26 printf("\n"); 27 } 28 } 29 void init(int x[][N],int n,int value) 30 { 31 int i,j; 32 for(i=0;i<n;++i) 33 for(j=0;j<n;++j) x[i][j]=value; 34 }
运行结果:
回答问题:
问题1:第二维的大小不能省略
问题2:函数的output功能是输出n行n列的数据,函数init的功能是将数组中的元素赋值
实验任务4:
task4.c源代码:
1 #include<stdio.h> 2 #define N 100 3 4 void input(int x[],int n); 5 float median(int x[],int n); 6 7 8 9 int main(){ 10 int x[N]; 11 int n; 12 double ans; 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 return 0; 19 } 20 21 22 void input(int x[], int n){ 23 int i; 24 for (int i = 0;i < n; i++){ 25 scanf("%d",&x[i]); 26 } 27 } 28 float median(int x[],int n){ 29 int i,j; 30 for(i=0;i<n-1;++i){ 31 for(j=0;j<n-i-1;j++){ 32 if(x[j]>x[j+1]){ 33 int temp = x[j]; 34 x[j]=x[j+1]; 35 x[j+1]=temp; 36 } 37 } 38 }
39 if(n%2!=0){ 40 return x[n/2]; 41 } 42 else{ 43 return (x[n/2-1]+x[n/2])/2.0; 44 } 45 }
运行结果:
实验任务5:
task5.c源代码:
1 #define N 100 2 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 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 printf("原始矩阵:\n"); 16 output(x, n); 17 18 rotate_to_right(x, n); 19 printf("变换后矩阵:\n"); 20 output(x, n); 21 22 return 0; 23 } 24 25 void input(int x[][N], int n){ 26 int i,j; 27 28 for(i=0;i<n;++i){ 29 for (j = 0;j<n;++j) 30 scanf("%d",&x[i][j]); 31 } 32 } 33 34 void output(int x[][N], int n){ 35 int i,j; 36 37 for(i = 0;i<n;++i){ 38 for(j=0;j<n;++j) 39 printf("%4d",x[i][j]); 40 41 printf("\n"); 42 43 } 44 } 45 46 void rotate_to_right(int x[][N],int n){ 47 int temp[N]; 48 int i,j ; 49 for (j=0;j<n;j++){ 50 temp[j]=x[j][n-1]; 51 for(i=n-1;i>0;i--){ 52 x[j][i]=x[j][i-1]; 53 } 54 x[j][0]=temp[j]; 55 } 56 }
运行结果:
实验任务6:
task6.c源代码:
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 13 printf("\n"); 14 } 15 return 0; 16 } 17 18 void dec_to_n(int x, int n) { 19 char result[100]; 20 int i = 0; 21 int j; 22 while (x > 0) { 23 int remainder = x % n; 24 if (n == 16 && remainder >= 10) { 25 if (remainder == 10) { 26 result[i++] = 'A'; 27 } else if (remainder == 11) { 28 result[i++] = 'B'; 29 } else if (remainder == 12) { 30 result[i++] = 'C'; 31 } else if (remainder == 13) { 32 result[i++] = 'D'; 33 } else if (remainder == 14) { 34 result[i++] = 'E'; 35 } else if (remainder == 15) { 36 result[i++] = 'F'; 37 } 38 } else { 39 result[i++] = remainder + '0'; 40 } 41 x /= n; 42 } 43 result[i] = '\0'; 44 for (j = i - 1; j >= 0; j--) { 45 printf("%c", result[j]); 46 } 47 printf("\n"); 48 }
运行结果:
实验任务7:
task7.c源代码:
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 9 int main(){ 10 int x[N][N]; 11 int n; 12 13 while(printf("输入n: "),scanf("%d",&n) !=EOF){ 14 printf("输入方阵:\n"); 15 input(x, n); 16 17 printf("输出方阵:\n"); 18 output(x, n); 19 20 if(is_magic(x, n)) 21 printf("是魔方矩阵\n\n"); 22 else 23 printf("不是魔方矩阵\n\n"); 24 } 25 return 0; 26 } 27 28 void input(int x[][N],int n){ 29 int i,j; 30 31 for(i = 0;i<n;++i){ 32 for(j = 0;j < n;++j) 33 scanf("%d",&x[i][j]); 34 35 } 36 } 37 38 void output(int x[][N],int n){ 39 int i,j; 40 for(i=0;i<n;++i){ 41 for(j=0;j<n;++j) 42 printf("%4d",x[i][j]); 43 printf("\n"); 44 } 45 } 46 int is_magic(int x[][N],int n){ 47 int sum = 0; 48 int m,i,j; 49 for (i=0;i<n;i++){ 50 sum+=x[0][i]; 51 } 52 for (i=0;i<n;i++){ 53 m=0; 54 for (j=0;j<n;j++){ 55 m+=x[i][j]; 56 } 57 if(m!=sum) 58 return 0; 59 } 60 m = 0; 61 for(i=0;i<n;i++){ 62 m+=x[i][i]; 63 } 64 if(m!=sum){ 65 return 0; 66 } 67 m=0; 68 for (i=0;i<n;i++){ 69 m+=x[i][n-1-i]; 70 } 71 if(m!=sum){ 72 return 0; 73 } 74 return 1; 75 }
运行结果: