美文网首页c语言
C Primer Plus第十章课后答案

C Primer Plus第十章课后答案

作者: 撼沧 | 来源:发表于2020-10-10 22:45 被阅读0次

    其他章节答案

    /*Project 1*/
    #include<stdio.h>
    #define MONTHS 12
    #define YEARS 5
    int main(void)
    {
    const float rain[YEARS][MONTHS] = 
    {
        {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
        {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
        {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
        {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
        {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
    };
    int year,month;
    float subtot,total;
    printf("TEAR RAINFALL (inches)\n");
    for(year = 0, total = 0; year < YEARS; year++)
    {
        for(month = 0,subtot = 0; month < MONTHS;month++)
            subtot += *(*(rain + year) + month);
        printf("%5d %15.1f\n", 2010 + year, subtot);
        total += subtot; /* total for all years */
    }
    printf("\nThe yearly average is %.1f inches.\n\n", total/YEARS);
    printf("MONTHLY AVERAGES:\n\n");
    printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
    printf(" Nov Dec\n");
    for (month = 0; month < MONTHS; month++)
    { 
        for (year = 0, subtot =0; year < YEARS; year++)
        subtot += *(*(rain + year) + month);
        printf("%4.1f ", subtot/YEARS);
    }
    printf("\n");
    return 0;
    } 
    /*Project 2*/
    #include<stdio.h>
    #define SIZE 5
    void copy_arr(double [],double [],int);
    void copy_ptr(double *,double *,int);
    void copy_ptrs(double *,double *,double *);
    int output(double []);
    int main(void)
    {
        double source[SIZE] = {1.1,2.2,3.3,4.4,5.5};
        double target1[SIZE];
        double target2[SIZE];
        double target3[SIZE];
        copy_arr(target1,source,SIZE);
        copy_ptr(target2,source,SIZE);
        copy_ptrs(target3,source,source + SIZE);
        output(target1);
        output(target2);
        output(target3);
        return 0;
    }
    void copy_arr(double  target[],double  source0[],int n)
    {
        int i;
        for(i = 0;i < n;i++)
            target[i] = source0[i];
    }
    void copy_ptr(double  *target,double  *source0,int n)
    {
        int i;
        for(i = 0;i < n;i++)
            *(target + i) = *(source0 + i);
    }
    void copy_ptrs(double  *target,double  *source0,double *end)
    {
        while(source0<end)
        {
            *target++ = *source0++;
        }
    }
    int output(double target[])
    {
        int i;
        for(i = 0;i < 5;i++)
            printf("%g ",target[i]);
        printf("\n");   
        return 0;
    }
    /*Project 3*/
    #include<stdio.h>
    int max(int []);
    int main(void)
    {
        int aa[] = {1,2,3,4,6,1,5,7,1};
        printf("%d",max(aa));
        return 0;
    }
    int max(int a[])
    {
        int n;
        int i = 0;
        n = a[i];
        for(i = 1;i != '\n';i++)
        {
            if(n < a[i])
                n = a[i];
        }
        return n;
    }
    /*Project 4*/
    #include<stdio.h>
    int index(double []);
    int main(void)
    {
        double aa[] = {1.3,2.1,3.4,4.2,6.2,2.1,4.5,4.67,9.31};
        printf("%d",index(aa));
        return 0;
    }
    int index(double a[])
    {
        double n;
        int m;
        int i = 0;
        n = a[i];
        m = i;
        for(i = 1;i != '\n';i++)
        {
            if(n < a[i])
            {
                n = a[i];
                m = i;
            }
        }
        return m;
    }
    /*Project 5*/
    #include<stdio.h>
    double index(double []);
    int main(void)
    {
        double aa[] = {1.3,2.1,3.4,4.2,0.6,2.1,4.5,4.67,9.31};
        printf("%g",index(aa));
        return 0;
    }
    double index(double a[])
    {
        double n,m;
        int i = 0;
        n = a[i];
        m = a[i];
        for(i = 1;i != '\n';i++)
        {
            if(n < a[i])
            {
                n = a[i];
            }
            if(m > a[i])
            {
                m > a[i];
            }
        }
        return n - m;
    }
    /*Project 6*/
    #include<stdio.h>
    void index(double [],int);
    int main(void)
    {
        double aa[] = {1.3,2.1,3.4,4.2,0.6,2.1,4.5,4.67,9.31};
        int i,n;
        n = sizeof(aa) / sizeof(aa[0]);
        index(aa,n);
        return 0;
    }
    void index(double a[],int n)
    {
        double b[n];
        int i;
        for(i = 0;i <= n;i++)
        {
            b[i] = a[n-1-i];
        }
        for(i = 0;i <= n;i++)
        {
            a[i] = b[i];
        }
        for(i = 0;i < n;i++)
        {
            printf("%g ",a[i]);
        }
    }
    /*Project 7*/
    #include<stdio.h>
    #define SIZE1 3
    #define SIZE2 3
    void copy_arr(double [],double [],int);
    int main(void)
    {
    double source[SIZE1][SIZE2] = {{1.2,3.5,5.3},{3,4.5,6.7},{2.3}};
    double target[SIZE1][SIZE2];
    int i,j;
    for(i = 0;i< SIZE1;i++)
        copy_arr(target[i],source[i],SIZE2);
    for(i = 0;i < SIZE1;i++)
    {
        for(j = 0;j < SIZE2;j++)
            printf("%5g ",target[i][j]);
        printf("\n");
    }
    return 0;
    }
    void copy_arr(double  target[],double  source0[],int n)
    {
        int i;
        for(i = 0;i < n;i++)
            target[i] = source0[i];
    }
    /*Project 8*/
    #include<stdio.h>
    #define SIZE1 7
    #define SIZE2 3
    #define NUMBER1 2
    #define NUMBER2 5
    void copy_arr(double [],double [],int);
    int main(void)
    {
        double source[SIZE1] = {1,3,5,6,7,3,2};
        double target[SIZE2];
        int i;
        copy_arr(target,source + NUMBER1,SIZE2);
        for(i = 0;i < SIZE2;i++)
            printf("%5g ",target[i]);
        printf("\n");
        return 0;
    }
    void copy_arr(double  target[],double  source0[],int n)
    {
        int i;
        for(i = 0;i < n;i++)
            target[i] = source0[i];
    }
    /*Project 9*/
    #include<stdio.h>
    double copy(int,int,double b[*][*]);
    double print(int,int,double d[*][*],double e[*][*]);
    int main(void) 
    {
        double a[3][5] = {{2.3,4.5,2.4,5.3,2.4},{4,3,4.2,4.5,2.3},{2.4,5.2,5.8,8.6,4.6}};
        copy(3,5,a);    //传递数组只需要输一个数组名就行了,不需要输中括号
        return 0;
     } 
     double copy(int n,int m,double b[n][m])
     {
        double c[n][m];
        for(int i = 0;i < n;i++)
            for(int j = 0;j < m;j++)
            {
                c[i][j] = b[i][j];
             }
        print(n,m,b,c);
        return 0;
     }
     double print(int n,int m,double d[n][m],double e[n][m])
     {
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < m;j++)
            {
                printf("%5g ",d[i][j]);
            }
            printf("\n");
        }
        printf("\n");
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < m;j++)
            {
                printf("%5g ",e[i][j]);
            }
            printf("\n");
        }
         return 0;  
     }
    /*Project 10*/
    #include<stdio.h>
    int add(int,int a[*],int b[*],int c[*]);
    #define N 4
    int main(void)
    {
        int a[N] = {2,4,5,8};
        int b[N] = {1,0,4,6};
        int c[N];
        add(N,a,b,c);
        for(int i = 0;i < N;i++)
        {
            printf("%3d ",c[i]);
        }
        return 0;
    }
    int add(int n,int a[n],int b[n],int c[n])
    {
        for(int i = 0;i < n;i++)
        {
            c[i] = a[i] + b[i];
        }
        return 0;
    }
    /*Project 11*/
    #include<stdio.h>
    #define ROWS 3
    #define LOWS 5
    int print(int,int b[][LOWS]);
    int print(int,int b[][LOWS]);
    int main(void)
    {
        int a[ROWS][LOWS] = {{1,3,4,5,6},{4,6,2,7,8},{4,3,6,7,2}};
        print(ROWS,a);
        doubles(ROWS,a);
        print(ROWS,a);
        return 0;
    }
    int doubles(int rows,int b[][LOWS])
    {
        for(int i = 0;i < rows;i++)
        {
            for(int j = 0;j < LOWS;j++)
            {
                b[i][j] = 2 * b[i][j];
            }
        }
        return 0;
    }
    int print(int rows,int b[][LOWS])
    {
        for(int i = 0;i < rows;i++)
        {
            for(int j = 0;j < LOWS;j++)
            {
                printf("%3d ",b[i][j]);
            }
            printf("\n");
        }
        printf("\n");
        return 0;
    }
    /*Project 12*/
    未做,简单又麻烦
    /*Project 13*/
    #include<stdio.h>
    #define ROWS 3
    #define LOWS 5
    int inputs(double a[ROWS][LOWS]);
    double means(double b[LOWS]);
    double meanss(double b[ROWS][LOWS]);
    double maxs(double b[ROWS][LOWS]);
    int main(void)
    {
        double a[ROWS][LOWS];
        //a 
        inputs(a);
        printf("\n你输入的数组:\n"); 
        for(int i = 0;i < ROWS;i++)
        {
            for(int j = 0;j < LOWS;j++)
            {
                printf("%g ",a[i][j]);
            }
            printf("\n");
        }
        printf("\n");
        //b
        for(int i = 0;i < ROWS;i++)
        {
            printf("第%d组平均数%g\n",i+1,means(a[i]));
        }
        printf("\n");
        //c
        printf("所有数据的平均数%g\n",meanss(a));
        printf("\n");
        //d
        printf("数据中的最大值%g\n",maxs(a));
        printf("\n");
        return 0;
    }
    int inputs(double a[ROWS][LOWS])
    {
        char y;
        for(int i = 0;i < ROWS;i++)
        {
            printf("请输入三组double类型的数(第%d组):",i+1);
            scanf("%lf %lf %lf %lf %lf",&a[i][0],&a[i][1],&a[i][2],&a[i][3],&a[i][4]);
        }
        return 0;
    }
    double means(double b[LOWS])
    {
        double m = 0;
        for(int i = 0;i < LOWS;i++)
        {
            m += b[i];
        }
        return m / LOWS;
    }
    double meanss(double b[ROWS][LOWS])
    {
        double m = 0;
        for(int i = 0;i < ROWS;i++)
        {
            for(int j = 0;j < LOWS;j++)
            {
                m += b[i][j];
            }
        }
        return m / (LOWS * ROWS);
    }
    double maxs(double b[ROWS][LOWS])
    {
        double m = 0;
        for(int i = 0;i < ROWS;i++)
        {
            for(int j = 0;j < LOWS;j++)
            {
                if(m < b[i][j])
                    m = b[i][j];
            }
        }
        return m;
    }
    /*Project 14*/
    #include<stdio.h>
    #define ROWS 3
    #define LOWS 5
    int inputs(int,int,double a[*][*]);
    double means(int,double b[*]);
    double meanss(int,int,double b[*][*]);
    double maxs(int,int,double b[*][*]);
    int main(void)
    {
        double a[ROWS][LOWS];
        //a 
        inputs(ROWS,LOWS,a);
        printf("\n你输入的数组:\n"); 
        for(int i = 0;i < ROWS;i++)
        {
            for(int j = 0;j < LOWS;j++)
            {
                printf("%g ",a[i][j]);
            }
            printf("\n");
        }
        printf("\n");
        //b
        for(int i = 0;i < ROWS;i++)
        {
            printf("第%d组平均数%g\n",i+1,means(LOWS,a[i]));
        }
        printf("\n");
        //c
        printf("所有数据的平均数%g\n",meanss(ROWS,LOWS,a));
        printf("\n");
        //d
        printf("数据中的最大值%g\n",maxs(ROWS,LOWS,a));
        printf("\n");
        return 0;
    }
    int inputs(int rows,int lows,double a[rows][lows])
    {
        char y;
        for(int i = 0;i < rows;i++)
        {
            printf("请输入三组double类型的数(第%d组):",i+1);
            scanf("%lf %lf %lf %lf %lf",&a[i][0],&a[i][1],&a[i][2],&a[i][3],&a[i][4]);
        }
        return 0;
    }
    double means(int lows,double b[lows])
    {
        double m = 0;
        for(int i = 0;i < lows;i++)
        {
            m += b[i];
        }
        return m / lows;
    }
    double meanss(int rows,int lows,double b[rows][lows])
    {
        double m = 0;
        for(int i = 0;i < rows;i++)
        {
            for(int j = 0;j < lows;j++)
            {
                m += b[i][j];
            }
        }
        return m / (lows * rows);
    }
    double maxs(int rows,int lows,double b[rows][lows])
    {
        double m = 0;
        for(int i = 0;i < rows;i++)
        {
            for(int j = 0;j < lows;j++)
            {
                if(m < b[i][j])
                    m = b[i][j];
            }
        }
        return m;
    }

    相关文章

      网友评论

        本文标题:C Primer Plus第十章课后答案

        本文链接:https://www.haomeiwen.com/subject/vfkspktx.html