C Primer Plus (中文版)第10章编程练习 参考答案(仅供参考~)

这篇具有很好参考价值的文章主要介绍了C Primer Plus (中文版)第10章编程练习 参考答案(仅供参考~)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

C Primer Plus (中文版)第10章编程练习 参考答案(仅供参考~)

🌴 C Primer Plus第10章编程练习~ 加油加油!🍭

☘️欢迎大家讨论 批评指正~

第1题

🍎1.修改程序清单10.7的rain.c程序,用指针进行计算(仍然要声明并初始化数组)。计算每年的总降水量、年平均降水量和5年中每月的平均降水量

/*
 * @Description:  修改程序清单10.7的rain.c程序,用指针进行计算
 (仍然要声明并初始化数组)。计算每年的总降水量、年平均降水量和5年中每月的平均降
水量 

#define MONTHS 12   // 一年的月份数
#define YEARS  5
 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 }
};
//输出:
    YEAR   RAINFALL  (inches)
    2010        32.4
    2011        37.9
    2012        49.8
    2013        44.0
    2014        32.9
    The yearly average is 39.4 inches.
    MONTHLY AVERAGES:
    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
    7.3 7.3 4.9 3.0 2.3 0.6 1.2 0.3 0.5 1.7 3.6 6.7

 * @Author: ~光~~
 * @Date: 2023-12-13 14:49:45
 * @LastEditTime: 2023-12-13 14:51:07
 * @LastEditors:  
 */

#include<stdio.h>
#define MONTHS 12   // 一年的月份数
#define YEARS  5
void ShowRain(const float x[][MONTHS]);
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 main(void){
    ShowRain(rain);
    return 0; 
}
void ShowRain(const float x[YEARS][MONTHS]){
    int i,j,k;
    int year=2010;
    float rainfall=0.0,sum_rainfall=0.0;
    float month[MONTHS]={0};
    //计算年降雨量
    printf("Year     RAINFALL(inches)\n");
    for(i=0;i<YEARS;i++){
        rainfall=0.0;
        for(j=0;j<MONTHS;j++){
            rainfall+=x[i][j];
            month[j]+=x[i][j];
        }
        sum_rainfall+=rainfall;
        printf("%d %12.2f\n",year,rainfall);
        year+=1;
    }
    printf("==================================\n");
    printf("The yearly average is %.2f inches.\n",sum_rainfall/YEARS);
    printf("MONTHLY AVERAGES:\n");
    //计算平均月降雨量
    printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
    for(i=0;i<MONTHS;i++){
        printf("%.1f ",month[i]/YEARS);
    }
    printf("\nMONTHLY Total:\n");
    //计算平均月降雨量
    printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
    for(i=0;i<MONTHS;i++){
        printf("%.1f ",month[i]);
    }

}

第2题

🍐编写一个程序,初始化一个double类型的数组,然后把该数组的内容
拷贝至3个其他数组中(在main()中声明这4个数组)。使用带数组表示法的函数进行第1份拷贝。
1️⃣使用带指针表示法和指针递增的函数进行第2份拷贝。
2️⃣把目标数组名、源数组名和待拷贝的元素个数作为前两个函数的参数。
3️⃣第3个函数以目标数组名、源数组名和指向源数组最后一个元素后面的元素的指针。
也就是说,给定以下声明,则函数调用如下所示:

double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5];
double target2[5];
double target3[5];
copy_arr(target1, source, 5);
copy_ptr(target2, source, 5);
copy_ptrs(target3, source, source + 5);
/*
 * @Description:  编写一个程序,初始化一个double类型的数组,然后把该数组的内容
拷贝至3个其他数组中(在main()中声明这4个数组)。
使用带数组表示法的函数进行第1份拷贝。

使用带指针表示法和指针递增的函数进行第2份拷贝。

把目标数组名、源数组名和待拷贝的元素个数作为前两个函数的参数。

第3个函数以目标数组名、源数组名和指向源数组最后一个元素后面的元素的指针。

也就是说,给定以下声明,则函数调用如下所示:

    double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
    double target1[5];
    double target2[5];
    double target3[5];
    copy_arr(target1, source, 5);
    copy_ptr(target2, source, 5);
    copy_ptrs(target3, source, source + 5);

 * @Author: ~光~~
 * @Date: 2023-12-13 15:16:50
 * @LastEditTime: 2023-12-13 15:16:56
 * @LastEditors:  
 */
#include<stdio.h>

void copy_arr(double target[], double source[],int x);//函数1
void copy_ptr(double *target, double *source, int x);//函数2
void copy_ptrs(double target[], double source[], double *p);//函数3

int main(void){
    double *p;
    int i,cnt;
    double source[5] = {1.10, 2.20, 3.30, 4.40, 5.50};
    double target1[5];
    double target2[5];
    double target3[5];
   
    copy_arr(target1, source, 5);
    printf("\n");
    copy_ptr(target2, source, 5);
    printf("\n");
    copy_ptrs(target3, source, source + 5);

    return 0; 
}

void copy_arr(double target[], double source[],int x){
    int i;
    for(i=0;i<x;i++){
        target[i]=source[i];
    }
    printf("first function,done!\n");
    printf("target is :");
    for(i=0;i<x;i++){
        printf("%.2f ",target[i]);
    }
}

void copy_ptr(double *target, double *source, int x){
    int i;
    for(i=0;i<x;i++){
        *target=*source;
        target++;
        source++;
    }
    printf("second function,done!\n");
    printf("target is :");
    for(i=0;i<x;i++){
        printf("%.2f ",target[i]);
    }
}

void copy_ptrs(double target[], double source[], double *p){
    int i,cnt;
    cnt=p-source;
    for(i=0;i<cnt;i++){
        *(target+i)=*(source+i);
    }
    printf("third function,done!\n");
    printf("target is :");
    for(i=0;i<cnt;i++){
        printf("%.2f ",target[i]);
    }
    printf("\n");
    printf("source is :");
    for(i=0;i<cnt;i++){
        printf("%.2f ",source[i]);
    }
}



第3题

🍌编写一个函数,返回储存在int类型数组中的最大值,并在一个简单的
程序中测试该函数

/*
 * @Description:  编写一个函数,返回储存在int类型数组中的最大值,并在一个简单的
程序中测试该函数
 * @Author: ~光~~
 * @Date: 2023-12-14 09:31:53
 * @LastEditTime: 2023-12-14 13:39:18
 * @LastEditors:  
 */
#include<stdio.h>
int maxx(int *x,int lenth);//找最大值

int main(void){
    int max,len;
    int x[10]={1,0,2,3,4,5,6,7,8,9};
    len=sizeof x/sizeof x[0];
    max=maxx1(x,len);
    printf("\nmax is %d\n",max);
    return 0; 
}

int maxx(int *x,int lenth){
    int i,max;
    max=x[0];
    for(i=0;i<lenth;i++){
        if(max<=x[i]){
            max=x[i];
        }
    }
    return max;
}


第4题

🍑4. 编写一个函数,返回储存在double类型数组中最大值的下标,并在一
个简单的程序中测试该函数

/*
 * @Description:  编写一个函数,返回储存在double类型数组中最大值的下标,并在一
个简单的程序中测试该函数
 * @Author: ~光~~
 * @Date: 2023-12-14 09:31:59
 * @LastEditTime: 2023-12-14 14:04:30
 * @LastEditors:  
 */
#include<stdio.h>
int max_j(double *x,int lenth);
int main(void){
    //此例的下标按第一个为1算起 不是0算起
    double x[4]={4,3,15,9};
    int len,max;
    len=sizeof x/sizeof x[0];
    max=max_j(x,len);
    printf("the maxnumber's subscript is %d\n",max);
    return 0; 
}
int max_j(double *x,int lenth){
    int i,j;
    double max;
    max=x[0];
    for(i=0;i<lenth;i++){
        if(max<x[i]){
            max=x[i];
            j=i;
        }
    }
    return j+1;
}

第5题

🌿编写一个函数,返回储存在double类型数组中最大值和最小值的差
值,并在一个简单的程序中测试该函数

/*
 * @Description:  编写一个函数,返回储存在double类型数组中最大值和最小值的差
值,并在一个简单的程序中测试该函数。
 * @Author: ~光~~
 * @Date: 2023-12-14 09:32:08
 * @LastEditTime: 2023-12-14 14:12:58
 * @LastEditors:  
 */
#include<stdio.h>
int Difference(double *x,int lenth);
int main(void){
    int len;
    double diff;
    double x[]={1,3,6,9,3,10};
    len=sizeof x/sizeof x[0];
    diff=Difference(x,len);
    printf("diff is %.2f\n",diff);
    return 0; 
}
int Difference(double *x,int lenth){
    int i,j;
    double max=x[0],min=x[0];
    for(i=0;i<lenth;i++){
        if(x[i]>=max) max=x[i];
        if(x[i]<=min) min=x[i];
    }
    printf("max is %.2f  min is %.2f\n",max,min);
    return max-min;
}

第6题

🌺6.编写一个函数,把double类型数组中的数据倒序排列,并在一个简单的程序中测试该函数。

/*
 * @Description:  编写一个函数,把double类型数组中的数据倒序排列,并在一个简单
的程序中测试该函数。
选择排序法:
https://zhuanlan.zhihu.com/p/123048793
 * @Author: ~光~~
 * @Date: 2023-12-14 09:32:12
 * @LastEditTime: 2023-12-14 14:59:05
 * @LastEditors:  
 */
#include<stdio.h>
void sort(double *x,int lenth);//采用选择排序法 从大到小
int main(void){
    int len,i;
    double x[]={1,6,5,3,9,2,1,45};
    len=sizeof x/sizeof x[0];
    printf("before the function is : ");
    for(i=0;i<len;i++){
        printf("%.2f ",x[i]);
    }

    sort(x,len);

    printf("\nafter the function is : ");
    for(i=0;i<len;i++){
        printf("%.2f ",x[i]);
    }
    return 0; 
}
void sort(double *x,int lenth){
    int i,j,max_j;
    int max,tmp;
    for(i=0;i<lenth-1;i++){
        max=x[i];
        // for(j=i+1;i<lenth-1;i++){ 自己看看自己再干什么  j和i 循环变量!!
        for(j=i;j<lenth;j++){    
            //if(x[j]>max){
            if(x[j]>=max){
                max_j=j;
                max=x[max_j];
            }
        }
        tmp=x[i];
        x[i]=max;
        // x[max_j]=x[i];关于交换位置 一定要注意!
        x[max_j]=tmp;
    }
    
}

第7题

🚀编写一个程序,初始化一个double类型的二维数组,使用编程练习2中
的一个拷贝函数把该数组中的数据拷贝至另一个二维数组中(因为二维数组
是数组的数组,所以可以使用处理一维数组的拷贝函数来处理数组中的每个
子数组)。

/*
 * @Description:  编写一个程序,初始化一个double类型的二维数组,使用编程练习2中
的一个拷贝函数把该数组中的数据拷贝至另一个二维数组中(因为二维数组
是数组的数组,所以可以使用处理一维数组的拷贝函数来处理数组中的每个
子数组)。
 * @Author: ~光~~
 * @Date: 2023-12-14 09:32:16
 * @LastEditTime: 2023-12-14 15:16:42
 * @LastEditors:  
 */
 
#include<stdio.h>
void copy_arr(double target[], double source[],int x);//函数1
int main(void){
    int i,j;
    double x[3][2]={{1,2},{3,4},{4,9}};//source
    double y[3][2];//target
    for(i=0;i<3;i++){
        copy_arr(y[i], x[i],2);
    }
    printf("y is: \n");
    for(i=0;i<3;i++){
        for (j=0;j<2;j++){
            printf("%.1f ",y[i][j]);
        }
        printf("\n");
    }
    return 0; 
}

void copy_arr(double target[], double source[],int x){
    int i;
    for(i=0;i<x;i++){
        target[i]=source[i];
    }
}

第8题

🐬 8.使用编程练习2中的拷贝函数,把一个内含7个元素的数组中第3~第5个元素拷贝至内含3个元素的数组中。该函数本身不需要修改,只需要选择
合适的实际参数(实际参数不需要是数组名和数组大小,只需要是数组元素
的地址和待处理元素的个数)

/*
 * @Description:  使用编程练习2中的拷贝函数,把一个内含7个元素的数组中第3~第5
个元素拷贝至内含3个元素的数组中。该函数本身不需要修改,只需要选择
合适的实际参数(实际参数不需要是数组名和数组大小,只需要是数组元素
的地址和待处理元素的个数)
 * @Author: ~光~~
 * @Date: 2023-12-14 09:32:26
 * @LastEditTime: 2023-12-14 15:18:14
 * @LastEditors:  
 */
#include<stdio.h>
void copy_arr(double target[], double source[],int x);//函数1
int main(void){
    int j;
    double x[7]={1,2,3,4,5,6,7};
    double y[3];
    
    copy_arr(y,x+2,3);

    printf("y is : \n");
    for (j=0;j<3;j++){
        printf("%.1f ",y[j]);
    }
    return 0; 
}
void copy_arr(double target[], double source[],int x){
    int i;
    for(i=0;i<x;i++){
        target[i]=source[i];
    }
}

第9题

🐋9.编写一个程序,初始化一个double类型的3×5二维数组,
1️⃣使用一个处理变长数组的函数将其拷贝至另一个二维数组中。
2️⃣还要编写一个以变长数组为形参的函数以显示两个数组的内容。
这两个函数应该能处理任意N×M数组
(如果编译器不支持变长数组,就使用传统C函数处理N×5的数组)。

/*
 * @Description:  编写一个程序,初始化一个double类型的3×5二维数组,

使用一个处理变长数组的函数将其拷贝至另一个二维数组中。
还要编写一个以变长数组为形参的函数以显示两个数组的内容。

这两个函数应该能处理任意N×M数组
(如果编译器不支持变长数组,就使用传统C函数处理N×5的数组)。
 * @Author: ~光~~
 * @Date: 2023-12-14 09:32:29
 * @LastEditTime: 2023-12-14 15:28:24
 * @LastEditors:  
 */
#include<stdio.h>
void copy(int x,int y,double target[x][y],double source[x][y]);
void display(int x,int y,double k[x][y]);
int main(void){
    double x[][5]={{1,2,3,4,5},{6,7,8,9,0},{11,12,13,14,15}};
    double y[][5]={0};
    printf("before copy, y is : \n");
    display(3,5,y);
    copy(3,5,y,x);
    printf("====================\n");
    printf("after copy, y is : \n");
    display(3,5,y);
    return 0; 
}
void copy(int x,int y,double target[x][y],double source[x][y]){
    int i ,j;
    for(i=0;i<x;i++){
        for(j=0;j<y;j++){
            target[i][j]=source[i][j];
        }
    }
}

void display(int x,int y,double k[x][y]){
    int i ,j;
    printf("the array is: \n");
    for(i=0;i<x;i++){
        for(j=0;j<y;j++){
            printf("%.2f ",k[i][j]);
        }
        printf("\n");
    }
}

第10题

🚩10.编写一个函数,把两个数组中相对应的元素相加,然后把结果储存到第 3 个数组中。也就是说,如果数组1中包含的值是2、4、5、8,数组2中包含的值是1、0、4、6,那么该函数把3、4、9、14赋给第3个数组。函数接受3个数组名和一个数组大小。在一个简单的程序中测试该函数。

/*
 * @Description: 10.编写一个函数,把两个数组中相对应的元素相加,然后把结果储存
到第 3 个数组中。也就是说,如果数组1中包含的值是2、4、5、8,数组2中
包含的值是1、0、4、6,那么该函数把3、4、9、14赋给第3个数组。函数接
受3个数组名和一个数组大小。在一个简单的程序中测试该函数。
 * @Author: ~光~~
 * @Date: 2023-12-14 09:32:33
 * @LastEditTime: 2023-12-15 09:23:47
 * @LastEditors:  
 */

#include<stdio.h>
void add(int *a,int *b,int *c,int len);
int main(void){
    int a[4]={2,4,5,8};
    int b[4]={1,0,4,6};
    int c[4];
    int len=4,i;
    printf("a is :");
    for(i=0;i<len;i++) printf("%d ",a[i]);
    printf("\nb is :");
    for(i=0;i<len;i++) printf("%d ",b[i]);
    add(a,b,c,len);
    printf("\nc is :");
    for(i=0;i<len;i++) printf("%d ",c[i]);
    return 0; 
}
void add(int *a,int *b,int *c,int len){
    int i;
    for(i=0;i<len;i++){
        c[i]=a[i]+b[i];
    }
}

第11题

🌈11.编写一个程序,声明一个int类型的3×5二维数组,并用合适的值初始化它。该程序打印数组中的值,然后各值翻倍(即是原值的2倍),并显示
出各元素的新值。编写一个函数显示数组的内容,再编写一个函数把各元素
的值翻倍。这两个函数都以函数名和行数作为参数

/*
 * @Description: 11.编写并测试Fibonacci()函数,该函数用循环代替递归计算斐波那契
数。
 * @Author: ~光~~
 * @Date: 2023-12-09 16:17:55
 * @LastEditTime: 2023-12-09 16:40:01
 * @LastEditors:  
 */
/*
 * @Description:  编写一个程序,声明一个int类型的3×5二维数组,并用合适的值初始
化它。该程序打印数组中的值,然后各值翻倍(即是原值的2倍),并显示
出各元素的新值。编写一个函数显示数组的内容,再编写一个函数把各元素
的值翻倍。这两个函数都以函数名和行数作为参数
 * @Author: ~光~~
 * @Date: 2023-12-14 09:32:38
 * @LastEditTime: 2023-12-15 09:39:45
 * @LastEditors:  
 */
#include<stdio.h>
void display(int a[][5],int row);
void doublee(int a[][5],int row);
int main(void){
    int a[3][5]={{1,2,3,4,5},{6,7,8,9,0},{10,11,12,13,14}};
    printf("before double,a is:\n");
    display(a,3);
    doublee(a,3);
    printf("after double,a is:\n");
    display(a,3);
    return 0; 
}
void display(int a[][5],int row){
    int i,j;
    for(i=0;i<row;i++){
        for(j=0;j<5;j++){
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
}
void doublee(int a[][5],int row){
    int i,j;
    for(i=0;i<row;i++){
        for(j=0;j<5;j++){
            a[i][j]=a[i][j]<<1;//左移相当于变成原来的两倍
        }
    }
}

第12题

🍋12. 重写程序清单10.7的rain.c程序,把main()中的主要任务都改成用函数来完成。下面是rain.c文件

#include <stdio.h>
#define MONTHS 12   // 一年的月份数
#define YEARS  5    // 年数
int main(void)
{
    // 用2010~2014年的降水量数据初始化数组
    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(" YEAR   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;  // 5年的总降水量
    }
    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++)
    {             // 每个月,5年的总降水量
    for (year = 0, subtot = 0; year < YEARS; year++)
    subtot += rain[year][month];
    printf("%4.1f ", subtot / YEARS);
    }
    printf("\n");
    return 0;
}

🍶程序部分

/*
 * @Description:  
 * @Author: ~光~~
 * @Date: 2023-12-14 09:32:41
 * @LastEditTime: 2023-12-15 09:54:40
 * @LastEditors:  
 */
/*
 * @Description:  重写程序清单10.7的rain.c程序,把main()中的主要任务都改成用函数来完成。

 * @Author: ~光~~
 * @Date: 2023-12-14 09:32:41
 * @LastEditTime: 2023-12-15 09:45:42
 * @LastEditors:  
 */
    #include <stdio.h>
    #define MONTHS 12   // 一年的月份数
    #define YEARS  5    // 年数
    void month_rain(const float rain[][MONTHS]);//计算各月降水总和
    void year_rain(const float rain[][MONTHS]);// 每个月,5年的总降水量
    int main(void)
    {
        // 用2010~2014年的降水量数据初始化数组
        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(" YEAR   RAINFALL  (inches)\n");
        month_rain(rain);

        printf("MONTHLY AVERAGES:\n\n");
        printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep Oct ");
        printf(" Nov  Dec\n");
        year_rain(rain);
        return 0;
    }

    void month_rain(const float rain[][MONTHS]){
        int year, month;
        float subtot, total;
        
        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;  // 5年的总降水量
        }
        printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);
    }
    void year_rain(const float rain[][MONTHS]){
        int year, month;
        float subtot, total;
        
        for (month = 0; month < MONTHS; month++)
        {             // 每个月,5年的总降水量
            for (year = 0, subtot = 0; year < YEARS; year++)
                subtot += rain[year][month];
            printf("%4.1f ", subtot / YEARS);
        }
        printf("\n");
    }

第13题

🚁13. 编写一个程序,提示用户输入3组数,每组数包含5个double类型的数
(假设用户都正确地响应,不会输入非数值数据)。该程序应完成下列任务。
a.把用户输入的数据储存在3×5的数组中
b.计算每组(5个)数据的平均值
c.计算所有数据的平均值
d.找出这15个数据中的最大值
e.打印结果
每个任务都要用单独的函数来完成(使用传统C处理数组的方式)。完
成任务b,要编写一个计算并返回一维数组平均值的函数,利用循环调用该
函数3次。对于处理其他任务的函数,应该把整个数组作为参数,完成任务c
和d的函数应把结果返回主调函数。

/*
 * @Description:  编写一个程序,提示用户输入3组数,每组数包含5个double类型的数
(假设用户都正确地响应,不会输入非数值数据)。该程序应完成下列任务。
    a.把用户输入的数据储存在3×5的数组中
    b.计算每组(5个)数据的平均值
    c.计算所有数据的平均值
    d.找出这15个数据中的最大值
    e.打印结果
    每个任务都要用单独的函数来完成(使用传统C处理数组的方式)。完
    成任务b,要编写一个计算并返回一维数组平均值的函数,利用循环调用该
    函数3次。对于处理其他任务的函数,应该把整个数组作为参数,完成任务c
    和d的函数应把结果返回主调函数。
 * @Author: ~光~~
 * @Date: 2023-12-14 09:33:10
 * @LastEditTime: 2023-12-15 11:02:18
 * @LastEditors:  
  double summ=0;//首先类型要对的上,其次要记得初始化你手头的每一个值。
  printf("the sum of the array are %.2f\n",summ(a[][5],3));//传进去的是指针!!!
 */
#include<stdio.h>
void enter(double a[][5],int row);
double avg(double a[]);
double summ_avg(double a[][5],int row);
double maxx(double a[][5],int row);
void print(double a[][5]);
int main(void){
    int i,j;
    // double a[3][5]={
    //         {1.00, 2.00 ,3.00 ,4.00 ,5.00},
    //         {12.00 ,3.00 ,4.00 ,5.00 ,6.00},
    //         {13.00, 4.00 ,5.00 ,6.00 ,7.00}
    //         }; 调试用例
    double a[3][5]={0};    
    //任务a
    enter(a,3);
    print(a); 
    return 0; 

}
void enter(double a[][5],int row){
    int i=0,j;
    for(i=0;i<row;i++){
        printf("please enter the %dth numbers(5 numbers): ",i+1);
        scanf("%lf %lf %lf %lf %lf",&a[i][0],&a[i][1],&a[i][2],&a[i][3],&a[i][4]);
        fflush(stdin);
    }
    
}
double avg(double a[5]){
    int i,j;
    double summ=0;//首先类型要对的上,其次要记得初始化你手头的每一个值。
    for(j=0;j<5;j++){
            summ+=a[j];
    }
    return summ/5.0; 
   
}
double summ_avg(double a[][5],int row){
    int i;
    double summ=0;
    for(i=0;i<row;i++){
        summ+=5*avg(a[i]);
    }
    return summ/(row*5.0);
}
double maxx(double a[][5],int row){
    int i,j;
    double max=a[0][5];
    for(i=0;i<row;i++){
        for(j=0;j<5;j++){
            if(a[i][j]>max) max=a[i][j];
        }
    }
    return max;
}

void print(double a[][5]){
    int i,j;
    double t;
    //任务a
    printf("\nthe numbers you enter are:\n");
    for(i=0;i<3;i++){
        for(j=0;j<5;j++){
            printf("%.2f ",a[i][j]);
        }
        printf("\n\n");
    }
    //任务b
    for(i=0;i<3;i++){
        t=avg(a[i]);
        printf("the averge of  %dth numbers are %.2f\n",i+1,t);
     }

    //任务c
    // printf("the sum of the array are %.2f\n",summ(a[][5],3));//传进去的是指针!!!
    printf("\nthe total average of the array are %.2f\n",summ_avg(a,3));

    //任务d
    printf("\nthe max number of the array is %.2f\n",maxx(a,3));
}

第14题

⚙️14.以变长数组作为函数形参,完成编程练习13。

/*
 * @Description:  以变长数组作为函数形参,完成编程练习13。
 * @Author: ~光~~
 * @Date: 2023-12-14 09:33:22
 * @LastEditTime: 2023-12-15 11:07:24
 * @LastEditors:  
 */
#include<stdio.h>
void enter(int row,int column,double a[row][column]);
double avg(double a[]);
double summ_avg(int row,int column,double a[row][column]);
double maxx(int row,int column,double a[row][column]);
void print(int row,int column,double a[row][column]);
int main(void){
    int i,j;
    // double a[3][5]={
    //         {1.00, 2.00 ,3.00 ,4.00 ,5.00},
    //         {12.00 ,3.00 ,4.00 ,5.00 ,6.00},
    //         {13.00, 4.00 ,5.00 ,6.00 ,7.00}
    //         }; 调试用例
    double a[3][5]={0};    
    //任务a
    enter(3,5,a);
    print(3,5,a); 
    return 0; 

}
void enter(int row,int column,double a[row][column]){
    int i=0,j;
    for(i=0;i<row;i++){
        printf("please enter the %dth numbers(5 numbers): ",i+1);
        scanf("%lf %lf %lf %lf %lf",&a[i][0],&a[i][1],&a[i][2],&a[i][3],&a[i][4]);
        fflush(stdin);
    }
    
}
double avg(double a[5]){
    int i,j;
    double summ=0;//首先类型要对的上,其次要记得初始化你手头的每一个值。
    for(j=0;j<5;j++){
            summ+=a[j];
    }
    return summ/5.0; 
   
}
double summ_avg(int row,int column,double a[row][column]){
    int i;
    double summ=0;
    for(i=0;i<row;i++){
        summ+=column*avg(a[i]);
    }
    return summ/(row*column);
}
double maxx(int row,int column,double a[row][column]){
    int i,j;
    double max=a[0][5];
    for(i=0;i<row;i++){
        for(j=0;j<column;j++){
            if(a[i][j]>max) max=a[i][j];
        }
    }
    return max;
}

void print(int row,int column,double a[row][column]){
    int i,j;
    double t;
    //任务a
    printf("\nthe numbers you enter are:\n");
    for(i=0;i<row;i++){
        for(j=0;j<column;j++){
            printf("%.2f ",a[i][j]);
        }
        printf("\n");
    }
    //任务b
    printf("\n");
    for(i=0;i<3;i++){
        t=avg(a[i]);
        printf("the averge of  %dth numbers are %.2f\n",i+1,t);
     }

    //任务c
    // printf("the sum of the array are %.2f\n",summ(a[][5],3));//传进去的是指针!!!
    printf("\nthe total average of the array are %.2f\n",summ_avg(3,5,a));

    //任务d
    printf("\nthe max number of the array is %.2f\n",maxx(3,5,a));
}

⛵️完成啦~
☘️如果有其他解法~ 欢迎大家讨论 批评指正~
🌈 此编程练习参考答案为本人所写,如有错误欢迎大家批评指正~~ 如转载请说明来源~

🌈ok,完结~(●’◡’●) 看到这里 点个赞叭 (●’◡’●)文章来源地址https://www.toymoban.com/news/detail-760013.html

到了这里,关于C Primer Plus (中文版)第10章编程练习 参考答案(仅供参考~)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 中文编程入门(Lua5.4.6中文版)第四章 Lua 流程控制

    Lua 编程语言中的流程控制就像推塔游戏战场上的智谋队长,挥舞着策略之剑,根据战场局势(条件语句)的演变,精准地指挥团队成员执行或猛攻或防守的操作。在这场代码与逻辑的对战中,当判定条件亮起 “true” 的胜利信号时,指定的战术动作会被果断地施展,类似“全

    2024年03月19日
    浏览(37)
  • 中文编程入门(Lua5.4.6中文版)第七章 Lua 字符串与模式

    在网游的符文之地中,我们可以通过神秘的“字符串法术”来处理和操作一系列字符。以下是如何在Lua的魔法卷轴上施展这种法术: 字符串的表示与召唤:  在Lua的魔法治则中,你可以运用三种方式召唤出一串字符(即字符串): 单引号(\\\')内的字符序列。 双引号(\\\")内的字符

    2024年03月19日
    浏览(29)
  • win10系统安装SQL Server2005中文版安装教程和下载地址

    说明:win10系统上安装sql 2005我试了不下10次,后来发现安装中途报错是缺少了一个替换文件sqlservr64.rar(32位系统上安装叫sqlservr32.rar).大家按照下面的教程一步步来操作都能安装成功,关键地方会加红标注,建议大家安装前先看下教程,关键的步骤在第13/14/25/30步。 SQL Serv

    2024年02月02日
    浏览(108)
  • 掌握Java JDK 1.8 API帮助文档中文版,事半功倍编程

    引言: Java是一门强大且广泛应用的编程语言,在开发过程中,准确地了解和使用Java标准库的API是非常重要的。JDK(Java Development Kit)是Java开发工具包,提供了丰富的API文档,其中包含了Java标准库中各种类和方法的详细说明。本文将为您介绍如何使用JDK 1.8 API帮助文档的中文

    2024年02月07日
    浏览(31)
  • C++ Primer Plus第二章编程练习答案

    答案仅供参考,实际运行效果取决于运行平台和运行软件 1.编写一个C++程序,它显示您的姓名和地址。 2.编写一个C程序它要求用户输入一个以 long 为单位的距离,然后将它转换为码(- ng等于220码) 3.编写1个C++程序它使用3个用户定义的函数(括mai()),并生成下面的输出Three blind

    2024年02月09日
    浏览(44)
  • C++ primer plus第七章编程练习答案

    1.编写一个程序,不断要求用户输入两个数,直到其中的一个为 0。对于每两个数,程序将使用一个南数来计算它们的调和平均数,并将结果返回给 main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下: 调和平均数=2.0*xy/(x + y) 2.编写一个程序,要求用

    2024年02月10日
    浏览(32)
  • C Primer Plus第九章编程练习答案

    学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出! 1.设计一个函数min(x, y),返回两个double类型值的较小值。在一个简单 的驱动程序中测试该函数。 2

    2024年02月06日
    浏览(28)
  • C Primer Plus第六章编程练习答案

    学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出! 1.编写一个程序,创建一个包含26个元素的数组,并在其中储存26个小 写字母。然后打印数组的所有

    2024年02月06日
    浏览(34)
  • C++ Primer Plus第五章编程练习答案

    答案仅供参考,实际运行效果取决于运行平台和运行软件 1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和则程序将出29之间所有整数的和为44 2.使用array对

    2024年02月09日
    浏览(38)
  • C Primer Plus第十四章编程练习答案

    学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出! 由于使用的是命令行参数常用于linux系统或者vscode,但此代码是运行于vs2022的,测试截图就不弄了。

    2024年02月07日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包