实验4

test 1

#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:是按行连续存放;这三者在字面上的值一样;x[0]和x[1]相差16个字节;16个字节是一行元素所占的字节数

 

test 2

#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的功能是计算去除最大值和最小值后所有数的平均值

test 3

#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;
}

问题1:第二维的大小不能省略

问题2:output功能是输出行数和列数相等的正方形矩阵;init功能是将value的值填入正方形矩阵中

test 4

#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);
    }

    system("pause");
    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;

    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==0)
        ans=(x[n/2-1]+x[n/2])/2.0;

    else
        ans=x[n/2-1/2];

    return ans;
}

 test 5

#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,i,j;
    for(i=0;i<n;++i){
        t=x[i][n-1];
        
        for(j=n-2;j>=0;--j){
            x[i][j+1]=x[i][j];
        }
        
        x[i][0]=t;
        
    }

}

test 6

#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);
        printf("\n");
        dec_to_n(x,8);
        printf("\n");
        dec_to_n(x,16);

        printf("\n");
    }

    return 0;
}

void dec_to_n(int x,int n){
    int j,a[N];
    int i=0;
    
    if(n==2||n==8){
        while(x!=0){
        a[i]=x%n;
        x=x/n;
        ++i;
        }

        for(j=i-1;j>=0;--j){
            printf("%d",a[j]);
        }
    }

    else if(n==16){
        while(x!=0){
        a[i]=x%n;
        x=x/n;
        ++i;
        }

        for(j=i-1;j>=0;--j){
            switch(a[j]){
                case 10:printf("A");break;
                case 11:printf("B");break;
                case 12:printf("C");break;
                case 13:printf("D");break;
                case 14:printf("E");break;
                case 15:printf("F");break;
                default:printf("%d",a[j]);
            
            }
        }
    }
    

}

test 7

 

#include<stdio.h>
#define N 100

void input(int x[][N],int n);
void output(int x[][N],int n);
int is_magic(int [][N],int n);

int main(){
    int x[N][N];
    int n;

    while(printf("输入:"),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 i,j;
    int sum1,sum2,sum3=0,sum4=0;


    for(i=0;i<n;++i){
        sum1=0,sum2=0;

        for(j=0;j<n;++j){
            sum1+=x[i][j];
            sum2+=x[j][i];
        }

        if(sum1!=sum2)
            return 0;
    }

    for(i=0;i<n;++i){
        sum3+=x[i][i];
        sum4+=x[i][n-i-1];
    }

    if(sum3!=sum4)
        return 0;

    return 1;
}

 

posted @ 2024-11-10 01:01  璐Luzi  阅读(7)  评论(0编辑  收藏  举报