实验四
#include<stdio.h> #define N 4 #define M 2 void test1(){ int x[N]={1,9,8,4}; int i; printf("sizeof(x)=%d\n",sizeof(x)); for(i=0;i<N;++i) printf("%p:%d\n",&x[i],x[i]); printf("x=%p\n",x); } void test2(){ int x[M][N]={{1,9,8,4},{2,0,4,9}}; int i,j; printf("sizeof(x)=%d\n",sizeof(x)); for(i=0;i<M;++i) for(j=0;j<N;++j) printf("%p:%d\n",&x[i][j],x[i][j]); printf("\n"); printf("x=%p\n",x); printf("x[0]=%p\n",x[0]); printf("x[1]=%p\n",x[1]); printf("\n"); } int main(){ printf("测试1:int型一维数组\n"); test1(); printf("\n测试2:int型二维数组\n"); test2(); return 0; }
问题1:是连续存放的,x与&x[0]是一样的
问题2:是按行连续存放的,是一样的,相差32,差值为二维数组所占的字节数
#include<stdio.h> #define N 100 void input(int x[],int n); double compute(int x[],int n); int main(){ int x[N]; int n,i; double ans; while(printf("Enter n:"),scanf("%d",&n)!=EOF){ input(x,n); ans=compute(x,n); printf("ans=%.2f\n\n",ans); } return 0; } void input(int x[],int n){ int i; for(i=0;i<n;++i) scanf("%d",&x[i]); } double compute(int x[],int n){ int i,high,low; double ans; high=low=x[0]; ans=0; for(i=0;i<n;++i){ ans+=x[i]; if(x[i]>high) high=x[i]; else if(x[i]<low) low=x[i]; } ans=(ans-high-low)/(n-2); return ans; }
问题:input:输入一维数组compute: 去除最大值和最小值取平均值
#include<stdio.h> #define N 100 void output(int x[][N],int n); void init(int x[][N],int n,int value); int main(){ int x[N][N]; int n,value; while(printf("Enter n and value:"),scanf("%d%d",&n,&value)!=EOF){ init(x,n,value); output(x,n); printf("\n"); } return 0; } void output(int x[][N],int n){ int i,j; for(i=0;i<n;++i){ for(j=0;j<n;++j) printf("%d ",x[i][j]); printf("\n"); } } void init(int x[][N],int n,int value){ int i,j; for(i=0;i<n;++i) for(j=0;j<n;++j) x[i][j]=value; }
问题一:列不能省略
问题二:output功能是逐行输出,init功能是逐行输入
#include <stdio.h> #define N 100 void input(int x[], int n); double median(int x[], int n); int main(){ int x[N]; int n; double ans; while(printf("Enter n: "),scanf("%d", &n)!=EOF){ input(x,n); ans = median(x,n); printf("ans = %g\n\n",ans); } return 0; } void input(int x[], int n) { int i; for(i = 0; i < n; ++i) scanf("%d", &x[i]); } double median(int x[],int n){ int i,j,t; double ans,a,b; for(i = 0;i<n-1;i++) for(j=0;j<n-1-i;j++) if(x[j]>x[j+1]){ t = x[j]; x[j] = x[j+1]; x[j+1] = t; } if(n%2==1) ans = x[n/2]; else{ a=x[n/2]; b=x[n/2-1]; ans = (a+b)/2; } return ans; }
#include <stdio.h> #define N 100 void input(int x[][N],int n); void output(int x[][N],int n); void rotate_to_right(int x[][N],int n); int main(){ int x[N][N]; int n; printf("输入n: "); scanf("%d",&n); input(x,n); printf("原始矩阵:\n"); output(x,n); rotate_to_right(x,n); printf("变换后矩阵:\n"); output(x,n); return 0; } void input(int x[][N],int n){ int i,j; for(i = 0; i < n; ++i){ for(j = 0;j<n;++j) scanf("%d",&x[i][j]); } } void output(int x[][N], int n){ int i,j; for(i = 0;i < n;++i){ for(j = 0; j<n; ++j) printf("%4d",x[i][j]); printf("\n"); } } void rotate_to_right(int x[][N],int n){ int t,j,i; for (i = 0; i < n; i++) { t = x[i][n-1]; for (j = n - 1; j > 0; j--) { x[i][j] = x[i][j-1]; } x[i][0] = t; } }
#include <stdio.h> #define N 100 void dec_to_n(int x, int n); int main(){ int x; while(printf("输入十进制整数: "),scanf("%d",&x) !=EOF){ dec_to_n(x, 2); dec_to_n(x, 8); dec_to_n(x, 16); printf("\n"); } return 0; } void dec_to_n(int x, int n){ int a[100]; int i=0; int m; if(n==2){ while(x!=0){ m=x%2; a[i++]=m; x/=2; } } else if(n==8){ while(x!=0){ m=x%8; a[i++]=m; x/=8; } } else if(n==16){ while(x!=0){ m=x%16; if(m<10) a[i++]=m; else a[i++]=m-10+'A'; x/=16; } } for (int j = i - 1; j >= 0; j--) { if (a[j] < 10) { printf("%d", a[j]); } else { printf("%c", (char)a[j]); } } printf("\n"); }
#include <stdio.h> #define N 100 void input(int x[][N], int n); void output(int x[][N], int n); int is_magic(int x[][N], int n); int main() { int x[N][N]; int n; while(printf("输入n: "), scanf("%d", &n) != EOF){ printf("输入方阵:\n"); input(x, n); printf("输出方阵:\n"); output(x, n); if(is_magic(x, n)) printf("是魔方矩阵\n\n"); else printf("不是魔方矩阵\n\n"); } return 0; } void input(int x[][N], int n) { int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) scanf("%d", &x[i][j]); } } void output(int x[][N], int n) { int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) printf("%4d", x[i][j]); printf("\n"); } } int is_magic(int x[][N], int n){ int t = n * (n * n + 1) / 2; int i, j; for (i = 0; i < n; i++) { int a = 0; for (j = 0; j < n; j++) { a += x[i][j]; } if (a!= t) { return 0; } } for (j = 0; j < n; j++) { int b = 0; for (i = 0; i < n; i++) { b += x[i][j]; } if (b!= t) { return 0; } } int c = 0; for (i = 0; i < n; i++) { c += x[i][i]; } if (c!= t) { return 0; } int d = 0; for (i = 0; i < n; i++) { d += x[i][n - 1 - i]; } if (d!= t) { return 0; } return 1; }