吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 867|回复: 5
收起左侧

[讨论] C Primer Plus 10章练习题

  [复制链接]
forestlxg 发表于 2023-3-24 08:37
[C] 纯文本查看 复制代码
#include<stdio.h>
#define MONTHS 12
#define YEARS 5
#define NUMS 10
#define N 3
#define M 5
#define X 4
double copy_arr(double target[],double source[],int i);
double copy_ptr(double target[],double source[],int i);
double copy_pstrs(double target[],double source[],double *p);
void new_printf(double *p,int i);   //第二题的打印公共函数
int max_num1(double source[],int i);
int max_num(int *p);
double max_min(double source[],int j);
void Reverse_order(double *p,int j);
void copy_Two_dim_array(int t_d_array1[3][3],int t_d_array2[3][3],int i,int j);
double valarr_copy(int n,int m,double val_arr1[N][M],double val_arr2[N][M]);
void vararr_printf(int n,int m,double val_arr1[N][M],double val_arr2[N][M]);
int sum_array3(int int_arr1[],int int_arr2[],int int_arr3[],int i);
int int3x5_printf(int int3x5_1[][5],int i);
int int3x5_double(int int3x5_1[][5],int int3x5_2[][5],int i);
double year_rainfall(float rain[][12],int i);
void input_arr(double input_arr3x5[][5],int i);           //输入3X5数组
double input_arr_avage(double input_arr3x5[][5],int i);     //行平均值  
double input_arr_avage_all(double input_arr3x5[][5],int i);//数组平均值
double input_arr_max(double input_arr3x5[][5],int i);//数组最大数
void input_arr_print(double input_arr3x5[][5],int p);//打印函数
int val_input_arr(int n,int m,int i,double val_input_arr[N][M]);//变长输入3X5数组
double val_input_arr_avage(int n,int m,int i,double val_input_arr[N][M]);     //变长行平均值  
double val_input_arr_avage_all(int n,int m,int i,double val_input_arr[N][M]);//变长数组平均值
double val_input_arr_max(int n,int m,int i,double val_input_arr[N][M]);//变长数组最大数
int val_input_arr_print(int n,int m,int i,double val_input_arr[N][M]);//变长数组打印
int main(void)
{

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}
},subtot,total, *ptr1;
int year,month,num,i,j,num1[NUMS]={1,3,5,2,4,100,21,44,31,0},int_arr1[X]={2,4,6,8},int_arr2[X]={1,0,4,6},int_arr3[X];
int t_d_array2[3][3];int int3x5_1[3][5]={{5,4,6,3,4},{2,5,6,88,4},{3,65,44,2,1}},int3x5_2[3][5];
int t_d_array1[3][3]=
{
    {111,222,333},
    {444,555,666},
    {777,888,999}
};
ptr1=rain[0];
num=YEARS*MONTHS;
double source[5]={1.1,2.2,3.3,4.4,5.5};
double s2[][2]={{1.1,2.2},{3.3,4.4},{5.5,6.6},{7.7,8.8},{9.9,10.10}};
double target1[5],target2[5],target3[5],opp,s3[5][2],(*p8)[2],(*pp8)[2],three_arr[3];  
double val_arr2[N][M],val_arr1[N][M]={{2.7,3.3,5.4,6.8,1.2},{55.2,1.2,11.4,22.1,54.6},{11.8,92.4,86.1,51.3,44.9}};
double val_arr_input3x5[N][M],input_arr3x5[3][5];
//10.1
printf("YEAR    RAINFALL    (inches)\n");
for(year=0,total=0;year<=YEARS;year++)
{   
    for(month=0,subtot=0;month<MONTHS;month++)
        subtot+=*(ptr1+year+month);
    printf("%5d %15.1f\n",2010+year,subtot);
    total+=subtot;
}
printf("\n年平均值%.1f.\n\n",total/YEARS);
printf("月平均值\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
for(month=0;month<MONTHS;month++)
{
    ptr1=ptr1+year;
    for(year=0,subtot=0;year<YEARS;year++)
        subtot+=*(ptr1+year+month);
    printf("%4.1f",subtot/YEARS);

}
printf("\n");
getchar();
getchar();

//10.2
printf("原数组是\n");
new_printf(source,5);
copy_arr(target1,source,5);
printf("第一份拷贝是\n");
new_printf(target1,5);
copy_ptr(target2,source,5);
printf("第二份拷贝是\n");
new_printf(target2,5);
copy_pstrs(target3,source,source+5);
printf("第三份拷贝是\n");
new_printf(target3,5);
printf("Done!");
getchar();
getchar();

//10.3
i=max_num(num1);
printf("数组中最大的值是%d\n",i);
getchar();
getchar();

//10.4
i=max_num1(source,5);
printf("数组中最大数的下标为%d\n",i);
getchar();
getchar();

//10.5
opp=max_min(source,5);
printf("数组中最大数与最小数的差为%.2f\n",opp);
getchar();
getchar();

//10.6
printf("原数组为\n");
for(i=0;i<5;i++)
    printf("%.2f\t",source[i]);
printf("\n");
printf("倒序后数组为\n");
Reverse_order(source,5);
copy_Two_dim_array(t_d_array1,t_d_array2,3,3);
getchar();
getchar();


//10.7
p8=s2,pp8=s3;
for(i=0;i<=4;i++)   //用指针打印原数组
    {
        for(j=0;j<2;j++)
            printf("%.2f\t",*(*(p8+i)+j));
        printf("\n");
    }
getchar();
for(i=0;i<=4;i++)
    {
        copy_ptr(*(pp8+i),*(p8+i),2);   // 单行拷贝数组,用第2题中的函数
        for(j=0;j<2;j++)
            printf("%.2f\t",*(*(pp8+i)+j));
        printf("\n");
    }


//10.8
copy_arr(three_arr,source,3);
for(i=0;i<3;i++)
        printf("%.2f\t",three_arr[i]);

//10.9
valarr_copy(N,M,val_arr1,val_arr2);
vararr_printf(N,M,val_arr1,val_arr2);

//10.10
sum_array3(int_arr1,int_arr2,int_arr3, X);

//10.11
int3x5_printf(int3x5_1,N);
int3x5_double(int3x5_1,int3x5_2,N);

//10.12
year_rainfall( rain,5);

//10.13
input_arr(input_arr3x5,3);
input_arr_print(input_arr3x5,3);    //此为打印函数,其它b.c.d项任务均在打印函数内完成。
//10.14
val_input_arr(N,M,3,val_arr_input3x5);
val_input_arr_print(N,M,3,val_arr_input3x5);
getchar();
getchar();
return 0;
}
double copy_arr(double target[],double source[],int i)
{
    int j;
    for(j=0;j<i;j++)
        target[j]=source[j];
    return 0;
}
double copy_ptr(double target[],double source[],int i)
{
    double *p,*q;
    int j;
    p=target;
    q=source;
    for(j=0;j<i;j++)
      *p++=*q++;        //*q++的运行顺序是先取*q的值 ,然后q指针向后移动一个存储位置。p指针同理。
                        //*++q的运行顺序是先把q指针向后移动一个存储位置,再取出*q的值
                        //(*q)++的运行顺序是先取出*q的值 ,再递增*q的值。
    return 0;
}
double copy_pstrs(double target[],double source[],double *p)
{
    int i,j;
    double *m,*n;
    
    m=target;
    n=source;
    for(i=4;i>=0;i--)
    {
        *(m+4-i)=*(p-1-i);      //因为P是指向源数组结尾后面的指针,首先要把指针向前移动一个存储位置才能源数据的最后一个元素。
                                //从最后一个元素开始拷贝数据。
    }
    return 0;
}
void new_printf(double *p,int i)    //实现打印功能的函数,不需要有返回值。
{
    int j;
    for(j=0;j<i;j++)
        printf("%.1f",*(p+j));
    printf("\n");
}
int max_num(int *p)
{
    int i,j;
    j=*(p+0);
    for(i=1;i<NUMS;i++)
        if(j<=*(p+i))
            j=*(p+i);
    return j;
}
int max_num1(double source[],int o)
{
    int j,i,k;
    double m,*p;
    p=source;
    m=source[0];
    for(j=0,k=0;j<=o;j++)
        if (m<source[j])
            {
                m=source[j];
                k=j;
            }
    return k;
}
double max_min(double source[],int j)
{
    int i;
    double m,n;
    m=source[0];    //初始化最大最小数
    n=m;
    for(i=0;i<j;i++)
    {
        if (m<source[i])
            m=source[i];
        if (n>source[i])
            n=source[i];
    }
    return (m-n);
}
void Reverse_order(double *p,int j)
{
    int i;
    for(i=(j-1);i>=0;i--)
        printf("%.2f\t",*(p+i));
    printf("\n");
}
void copy_Two_dim_array(int array1[3][3],int array2[3][3],int i,int j)
{
    int m,n;
    for(m=0;m<i;m++)
        for(n=0;n<j;n++)
            array2[m][n]=array1[2-m][2-n];  //倒序,方便检查
    for(m=0;m<i;m++)
        {
            for(n=0;n<j;n++)
                printf("%2d\t",array2[m][n]);
            printf("\n");
        }
}
double valarr_copy(int n,int m,double var_arr1[N][M],double var_arr2[N][M])
{
    int i,j;
    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            *(*(var_arr2+i)+j)=*(*(var_arr1+i)+j);
    return 0;
}
void vararr_printf(int n,int m,double val_arr1[N][M],double val_arr2[N][M])

{
    int i,j;

    printf("原数组:\n");
     for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                printf("%.2f\t",val_arr1[i][j]);
            printf("\n");
        }
    printf("\n拷贝数组\n");
     for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                printf("%.2f\t",val_arr2[i][j]);
            printf("\n");
        }        
}
int sum_array3(int int_arr1[],int int_arr2[],int int_arr3[],int i)
{
    int j;
    for(j=0;j<i;j++)
        int_arr3[j]=int_arr1[j]+int_arr2[j];
    printf("数组1\n");
    for(j=0;j<i;j++)
        printf("%4d",int_arr1[j]);
    printf("\n");
        printf("数组2\n");
    for(j=0;j<i;j++)
        printf("%4d",int_arr2[j]);
    printf("\n");
        printf("数组3\n");
    for(j=0;j<i;j++)
        printf("%4d",int_arr3[j]);
    printf("\n");
    return 0;
}
int int3x5_printf(int int3x5_1[][5],int i)
{
    int j,m;
    printf("原数组\n");
    for(j=0;j<i;j++)
        {
            for(m=0;m<5;m++)
                printf("%4d",int3x5_1[j][m]);
            printf("\n");
        }
    return 0;    
}
int int3x5_double(int int3x5_1[][5],int int3x5_2[][5],int i)
{
    int j,m;
    printf("\n翻倍数组\n");
    for(j=0;j<i;j++)
        {
            for(m=0;m<5;m++)
                printf("%4d",int3x5_2[j][m]=int3x5_1[j][m]*2);
            printf("\n");
        }
    return 0;    
}
double year_rainfall(float rain[][12],int i)        //变长数组计算降水量
{
    int year,month,j;
    float total,month_sub;
    printf("\t年份\t\t降雨量(单位:英寸)\n");
    for(year=0,total=0;year<i;year++)
    {
        for(month=0,month_sub=0;month<MONTHS;month++)
            month_sub+=rain[year][month];
        printf("\t%d\t\t%.1f\n",2010+year,month_sub);
        total+=month_sub;
    }
    printf("\t年平均降水量:%.1f英寸\n\n",total/YEARS);
    printf("\t月平均降水量:\n");
    printf("\tJan\tFeb\tMar\tApr\tMay\tJun\tJul\tAug\tSep\tOct\tNov\tDec\n");
    for(month=0,total=0;month<MONTHS;month++)
    {
        for(year=0;year<i;year++)
            month_sub+=rain[year][month];
        printf("\t%.1f",month_sub/YEARS);
    }
    return 0;

}
void input_arr(double input_arr3x5[][5],int i)          //输入3X5数组
{
    int m,n;
    int *p;
    //p=input_arr3x5;
    printf("请输入一个3行5列的数组:\n");
    for(m=0;m<i;m++)
        for(n=0;n<5;n++)
        {
            scanf("%lf",&input_arr3x5[m][n]);
            getchar();
        }
    printf("\n\n");
}
double input_arr_avage(double input_arr3x5[][5],int i)   //行平均值  
{
    double m=0;
    int j;
        for(j=0;j<5;j++)
            m+=input_arr3x5[i][j];
    return m/5;
}
double input_arr_avage_all(double input_arr3x5[][5],int i)//数组平均值
{
    double m=0;
    int j,k;
    for(j=0;j<i;j++)
        for(k=0;k<5;k++)
            m+=input_arr3x5[j][k];
    return m/15;
}
double input_arr_max(double input_arr3x5[][5],int i)//数组最大数
{
    double max=0;
    int j,k;
    for(j=0;j<i;j++)
        for(k=0;k<5;k++)
            if (max<=input_arr3x5[j][k])
                max=input_arr3x5[j][k];
    return max;
}
void input_arr_print(double input_arr3x5[][5],int p)        //数组打印
{
    int rows,cols,i;
    printf("刚才输入的数组如下:\n");
    for(rows=0;rows<p;rows++)
       {
           for(cols=0;cols<5;cols++)
                printf("%.2f\t",input_arr3x5[rows][cols]);
            printf("\n");
       }  
    printf("\n");
    for(i=1;i<=3;i++)
        printf("数组第%d行的平均值为%.2f\n",i,input_arr_avage(input_arr3x5,i-1));
    printf("\n\n数组的平均值为%.2f\n",input_arr_avage_all(input_arr3x5,3));
    printf("\n\n数组的最大值为%.2f\n",input_arr_max(input_arr3x5,3));
}
int val_input_arr(int n,int m,int i,double val_arr_input3x5[N][M])      //变长数组输入
{
    int j,k;
    printf("请输入一个3行5列的数组:\n");
    for(j=0;j<i;j++)
        for(k=0;k<5;k++)
        {
            scanf("%lf",&val_arr_input3x5[j][k]);
            getchar();
        }
    printf("\n\n");
    return 0;
}
double val_input_arr_avage(int n,int m,int i,double val_arr_input3x5[N][M])     //变长数组行平均值 
{
    double s=0;
    int j;
        for(j=0;j<5;j++)
            s+=val_arr_input3x5[i][j];
    return s/5;
} 
double val_input_arr_avage_all(int n,int m,int i,double val_arr_input3x5[N][M])//变长数组数组平均值
{
    double s=0;
    int j,k;
    for(j=0;j<i;j++)
        for(k=0;k<5;k++)
            s+=val_arr_input3x5[j][k];
    return s/15;    
}
double val_input_arr_max(int n,int m,int i,double val_arr_input3x5[N][M])//变长数组数组最大数
{
    double max=0;
    int j,k;
    for(j=0;j<i;j++)
        for(k=0;k<5;k++)
            if (max<=val_arr_input3x5[j][k])
                max=val_arr_input3x5[j][k];
    return max;    
}
int val_input_arr_print(int n,int m,int i,double val_arr_input3x5[N][M])//变长数组打印函数
{
    int rows,cols,f;
    printf("刚才输入的数组如下:\n");
    for(rows=0;rows<i;rows++)
       {
           for(cols=0;cols<5;cols++)
                printf("%.2f\t",val_arr_input3x5[rows][cols]);
            printf("\n");
       }  
    printf("\n");
    for(f=1;f<=3;f++)
        printf("数组第%d行的平均值为%.2f\n",f,val_input_arr_avage(N,M,f-1,val_arr_input3x5));
    printf("\n\n数组的平均值为%.2f\n",val_input_arr_avage_all(N,M,3,val_arr_input3x5));
    printf("\n\n数组的最大值为%.2f\n",val_input_arr_max(N,M,3,val_arr_input3x5));   
    return 0; 
}

第十章练习题.txt

13.59 KB, 下载次数: 0, 下载积分: 吾爱币 -1 CB

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Supermexyh 发表于 2023-3-24 09:32
?你是想发题目还是求答案,我刚好有这本练习题的实体书
 楼主| forestlxg 发表于 2023-3-24 09:50
Supermexyh 发表于 2023-3-24 09:32
?你是想发题目还是求答案,我刚好有这本练习题的实体书

都不是。就是把练习题做了。发出来。还没看配套的习题答案。把全书的都做了。再看看习题答案。
sht281 发表于 2023-3-24 14:33
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 00:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表